PRAGMA foreign_keys = OFF;

CREATE TABLE IF NOT EXISTS "candidate_experience_v2" (
  "experience_id" TEXT PRIMARY KEY,
  "candidate_id" TEXT NOT NULL REFERENCES "candidate"("candidate_id") ON DELETE CASCADE,
  "employer_name" TEXT,
  "role_title" TEXT,
  "department" TEXT,
  "start_date" DATE,
  "end_date" DATE,
  "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  "last_updated_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  "country" TEXT,
  "city" TEXT,
  "facility_type" TEXT
);

INSERT INTO "candidate_experience_v2" (
  "experience_id",
  "candidate_id",
  "employer_name",
  "role_title",
  "department",
  "start_date",
  "end_date",
  "created_at",
  "last_updated_at",
  "country",
  "city",
  "facility_type"
)
SELECT
  "experience_id",
  "candidate_id",
  "employer_name",
  "role_title",
  "department",
  "start_date",
  "end_date",
  "created_at",
  "last_updated_at",
  "country",
  "city",
  "facility_type"
FROM "candidate_experience";

DROP TABLE "candidate_experience";
ALTER TABLE "candidate_experience_v2" RENAME TO "candidate_experience";

CREATE INDEX IF NOT EXISTS "idx_candidate_experience_candidate_id"
  ON "candidate_experience" ("candidate_id");

CREATE TABLE IF NOT EXISTS "recognition_dossier" (
  "dossier_id" TEXT PRIMARY KEY,
  "candidate_id" TEXT NOT NULL REFERENCES "candidate"("candidate_id") ON DELETE CASCADE,
  "target_state" TEXT NOT NULL,
  "target_profession" TEXT NOT NULL,
  "status" TEXT NOT NULL DEFAULT 'draft',
  "rules_version" TEXT,
  "input_fingerprint" TEXT,
  "generated_at" TIMESTAMP,
  "package_file_path" TEXT,
  "submission_guide_json" JSON NOT NULL DEFAULT '{}',
  "checklist_json" JSON NOT NULL DEFAULT '[]',
  "blocking_issues_json" JSON NOT NULL DEFAULT '[]',
  "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "last_updated_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE UNIQUE INDEX IF NOT EXISTS "idx_recognition_dossier_candidate_state_profession"
  ON "recognition_dossier" ("candidate_id", "target_state", "target_profession");

CREATE INDEX IF NOT EXISTS "idx_recognition_dossier_candidate_id"
  ON "recognition_dossier" ("candidate_id");

CREATE INDEX IF NOT EXISTS "idx_recognition_dossier_status"
  ON "recognition_dossier" ("status");

CREATE TABLE IF NOT EXISTS "recognition_dossier_document" (
  "document_id" TEXT PRIMARY KEY,
  "dossier_id" TEXT NOT NULL REFERENCES "recognition_dossier"("dossier_id") ON DELETE CASCADE,
  "candidate_id" TEXT NOT NULL REFERENCES "candidate"("candidate_id") ON DELETE CASCADE,
  "category" TEXT NOT NULL,
  "title" TEXT,
  "note" TEXT,
  "file_path" TEXT NOT NULL,
  "file_name" TEXT NOT NULL,
  "file_size" INTEGER,
  "mime_type" TEXT,
  "uploaded" BOOLEAN NOT NULL DEFAULT 0,
  "document_status" TEXT NOT NULL DEFAULT 'Unverified',
  "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "last_updated_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS "idx_recognition_dossier_document_dossier_id"
  ON "recognition_dossier_document" ("dossier_id");

CREATE INDEX IF NOT EXISTS "idx_recognition_dossier_document_candidate_id"
  ON "recognition_dossier_document" ("candidate_id");

CREATE INDEX IF NOT EXISTS "idx_recognition_dossier_document_category"
  ON "recognition_dossier_document" ("category");

PRAGMA foreign_keys = ON;
