const sqlite3 = require("sqlite3").verbose();
const path = require('path');

const dbPath = process.env.DATABASE_PATH || '../';
const db = new sqlite3.Database(path.join(dbPath, 'database.sqlite'));

db.serialize(() => {
  db.run(`
    CREATE TABLE IF NOT EXISTS sheeps (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      group_id INTEGER,
      name TEXT,
      icon TEXT,
      uuid TEXT,
      uuid_manager TEXT,
      mode INTEGER DEFAULT 0,
      mode_title TEXT DEFAULT 'Користувач',
      FOREIGN KEY (group_id) REFERENCES groups(group_number)
    )
  `);

  db.run(`
    CREATE TABLE IF NOT EXISTS possibilities (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      sheep_id INTEGER,
      can_add_sheeps INTEGER DEFAULT 0,
      can_view_sheeps INTEGER DEFAULT 0,
      can_add_territory INTEGER DEFAULT 0,
      can_view_territory INTEGER DEFAULT 0,
      can_manager_territory INTEGER DEFAULT 0,
      can_add_stand INTEGER DEFAULT 0,
      can_view_stand INTEGER DEFAULT 0,
      can_manager_stand INTEGER DEFAULT 0,
      can_add_schedule INTEGER DEFAULT 0,
      can_view_schedule INTEGER DEFAULT 0,
      FOREIGN KEY (sheep_id) REFERENCES sheeps(id)
    )
  `);

  db.run(`
    CREATE TABLE IF NOT EXISTS groups (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      group_number INTEGER UNIQUE,
      share_hash TEXT
    )
  `);

  db.run(`
    CREATE TABLE IF NOT EXISTS subscription (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      sheep_id INTEGER,
      endpoint TEXT,
      keys TEXT,
      device_name TEXT,
      device_model TEXT,
      created_at TIMESTAMP,
      FOREIGN KEY (sheep_id) REFERENCES sheeps(id)
    )
  `);

  db.run(`
    CREATE TABLE IF NOT EXISTS badges (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      sheep_id INTEGER,
      quantity INTEGER,
      FOREIGN KEY (sheep_id) REFERENCES sheeps(id)
    )
  `);

  db.run(`
    CREATE TABLE IF NOT EXISTS house (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      title TEXT,
      number TEXT,
      points TEXT DEFAULT '[]',
      points_number TEXT DEFAULT '[]',
      geo TEXT DEFAULT '[]',
      osm_id TEXT DEFAULT '[]',
      settlement TEXT,
      description TEXT,
      created_at TIMESTAMP,
      updated_at TIMESTAMP
    )
  `);

  db.run(`
    CREATE TABLE IF NOT EXISTS entrance (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      house_id INTEGER,
      entrance_number INTEGER,
      title TEXT,
      description TEXT,
      created_at TIMESTAMP,
      updated_at TIMESTAMP,
      FOREIGN KEY (house_id) REFERENCES house(id)
    )
  `);

  db.run(`
    CREATE TABLE IF NOT EXISTS entrance_history (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      entrance_id INTEGER,
      name TEXT,
      date_start TIMESTAMP,
      date_end TIMESTAMP,
      group_id INTEGER,
      sheep_id INTEGER,
      working INTEGER DEFAULT 0,
      FOREIGN KEY (entrance_id) REFERENCES entrance(id),
      FOREIGN KEY (sheep_id) REFERENCES sheeps(id),
      FOREIGN KEY (group_id) REFERENCES groups(group_number)
    )
  `);

  db.run(`
    CREATE TABLE IF NOT EXISTS apartments (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      entrance_id INTEGER,
      apartment_number INTEGER,
      title TEXT,
      floors_number INTEGER,
      status INTEGER,
      description TEXT,
      sheep_id INTEGER,
      updated_at TIMESTAMP,
      FOREIGN KEY (entrance_id) REFERENCES entrance(id),
      FOREIGN KEY (sheep_id) REFERENCES sheeps(id)
    )
  `);

  db.run(`
    CREATE TABLE IF NOT EXISTS apartments_history (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      sheep_id INTEGER,
      apartments_id INTEGER,
      status INTEGER,
      description TEXT,
      created_at TIMESTAMP,
      FOREIGN KEY (sheep_id) REFERENCES sheeps(id),
      FOREIGN KEY (apartments_id) REFERENCES apartments(id)
    )
  `);

  db.run(`
    CREATE TABLE IF NOT EXISTS homestead (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      title TEXT,
      number TEXT,
      points TEXT DEFAULT '[]',
      geo TEXT DEFAULT '[]',
      zoom INTEGER DEFAULT 18,
      osm_id TEXT DEFAULT '[]',
      settlement TEXT,
      description TEXT,
      created_at TIMESTAMP,
      updated_at TIMESTAMP
    )
  `);

  db.run(`
    CREATE TABLE IF NOT EXISTS homestead_history (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      homestead_id INTEGER,
      name TEXT,
      date_start TIMESTAMP,
      date_end TIMESTAMP,
      group_id INTEGER,
      sheep_id INTEGER,
      working INTEGER DEFAULT 0,
      FOREIGN KEY (homestead_id) REFERENCES homestead(id),
      FOREIGN KEY (sheep_id) REFERENCES sheeps(id),
      FOREIGN KEY (group_id) REFERENCES groups(group_number)
    )
  `);

  db.run(`
    CREATE TABLE IF NOT EXISTS meetings_schedule (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      date TIMESTAMP,
      type INTEGER,
      name TEXT,
      sheep_id INTEGER,
      title TEXT,
      number TEXT,
      FOREIGN KEY (sheep_id) REFERENCES sheeps(id)
    )
  `);

  db.run(`
    CREATE TABLE IF NOT EXISTS stand_list (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      title TEXT,
      geo TEXT DEFAULT '[]',
      hour_start INTEGER DEFAULT 9,
      hour_end INTEGER DEFAULT 18,
      quantity_sheep INTEGER DEFAULT 2,
      week_days TEXT DEFAULT '[0, 1, 2, 3, 4, 5, 6]',
      processing_time REAL DEFAULT 1,
      status INTEGER DEFAULT 0,
      updated_at TIMESTAMP,
      created_at TIMESTAMP
    )
  `);

  db.run(`
    CREATE TABLE IF NOT EXISTS stand_schedule (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      stand_id INTEGER,
      date TIMESTAMP,
      hour INTEGER,
      sheep_id INTEGER,
      number_sheep TEXT,
      updated_at TIMESTAMP,
      created_at TIMESTAMP,
      FOREIGN KEY (stand_id) REFERENCES stand_list(id),
      FOREIGN KEY (sheep_id) REFERENCES sheeps(id)
    )
  `);

  db.run(`
    CREATE TABLE IF NOT EXISTS stand_schedule_history (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      stand_schedule_id INTEGER,
      sheep_id INTEGER,
      created_at TIMESTAMP,
      FOREIGN KEY (stand_schedule_id) REFERENCES stand_schedule(id),
      FOREIGN KEY (sheep_id) REFERENCES sheeps(id)
    )
  `);
});

module.exports = db;