PRAGMA foreign_keys = ON;

CREATE TABLE IF NOT EXISTS research_projects (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  slug TEXT NOT NULL UNIQUE,
  title TEXT NOT NULL,
  abstract TEXT NOT NULL,
  keywords TEXT NOT NULL DEFAULT '[]',
  organization TEXT,
  standards_org TEXT,
  period TEXT,
  funder TEXT,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS research_areas (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name_en TEXT NOT NULL,
  name_kr TEXT,
  display_order INTEGER NOT NULL DEFAULT 0
);

CREATE TABLE IF NOT EXISTS members (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name_ko TEXT NOT NULL,
  name_en TEXT NOT NULL,
  degree TEXT NOT NULL CHECK (degree IN ('Professor','PhD','PhDCandidate','PhDStudent','MSCandidate','MSStudent')),
  affiliation TEXT,
  email TEXT,
  is_advisor BOOLEAN NOT NULL DEFAULT 0,
  display_order INTEGER NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS alumni (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name_ko TEXT NOT NULL,
  name_en TEXT NOT NULL,
  degree TEXT NOT NULL CHECK (degree IN ('PhD','MS')),
  graduated_at TEXT NOT NULL,
  major TEXT,
  current_position TEXT,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS publications (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  category TEXT NOT NULL CHECK (category IN ('intl-journal-conf','domestic-journal-conf')),
  title TEXT NOT NULL,
  authors TEXT,
  venue TEXT NOT NULL,
  volume TEXT,
  published_at TEXT NOT NULL,
  kci BOOLEAN NOT NULL DEFAULT 0,
  doi TEXT,
  is_highlight BOOLEAN NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_publications_category ON publications(category);
CREATE INDEX IF NOT EXISTS idx_publications_highlight ON publications(is_highlight) WHERE is_highlight = 1;

CREATE TABLE IF NOT EXISTS patents (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  title TEXT NOT NULL,
  inventors TEXT NOT NULL,
  application_no TEXT NOT NULL,
  application_at TEXT NOT NULL,
  registration_no TEXT,
  registration_at TEXT,
  country TEXT NOT NULL,
  published_at TEXT NOT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS semesters (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  year INTEGER NOT NULL,
  term TEXT NOT NULL CHECK (term IN ('Spring','Fall')),
  UNIQUE(year, term)
);

CREATE TABLE IF NOT EXISTS courses (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  semester_id INTEGER NOT NULL REFERENCES semesters(id) ON DELETE CASCADE,
  code TEXT NOT NULL,
  name_en TEXT NOT NULL,
  name_kr TEXT,
  note TEXT,
  display_order INTEGER NOT NULL DEFAULT 0
);
CREATE INDEX IF NOT EXISTS idx_courses_semester ON courses(semester_id);

CREATE TABLE IF NOT EXISTS standards_bodies (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  org TEXT NOT NULL,
  full_name TEXT NOT NULL,
  scope TEXT NOT NULL CHECK (scope IN ('international','domestic')),
  period TEXT NOT NULL,
  role TEXT,
  display_order INTEGER NOT NULL DEFAULT 0
);

CREATE TABLE IF NOT EXISTS standard_documents (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  body_id INTEGER NOT NULL REFERENCES standards_bodies(id) ON DELETE CASCADE,
  wg TEXT NOT NULL,
  project_name TEXT NOT NULL,
  title TEXT NOT NULL,
  doc_ref TEXT NOT NULL,
  status TEXT NOT NULL CHECK (status IN ('IS','TS','TR','CDV','WDTR','InProgress')),
  published_at TEXT
);
CREATE INDEX IF NOT EXISTS idx_stddocs_body ON standard_documents(body_id);
