CREATE TYPE category_code AS ENUM (
    'pullet_open',
    'pullet_close',
    'produksi_open',
    'produksi_close'
);

-- MASTER TABLES

CREATE TABLE employees (
  id          bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  name        varchar NOT NULL,
  is_active   boolean NOT NULL DEFAULT true,
  created_at  timestamptz NOT NULL DEFAULT now(),
  updated_at  timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE employee_kandangs (
  id           bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  employee_id  bigint NOT NULL,
  kandang_id   bigint NOT NULL,
  created_at   timestamptz NOT NULL DEFAULT now(),
  updated_at   timestamptz NOT NULL DEFAULT now(),

  CONSTRAINT fk_employee_kandangs_employee
    FOREIGN KEY (employee_id) REFERENCES employees(id)
    ON DELETE CASCADE,

  CONSTRAINT fk_employee_kandangs_kandang
    FOREIGN KEY (kandang_id) REFERENCES kandangs(id)
    ON DELETE CASCADE,

  CONSTRAINT uq_employee_kandangs UNIQUE (employee_id, kandang_id)
);

-- PHASE & CHECKLIST

CREATE TABLE phases (
  id          bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  name        varchar NOT NULL,
  is_active   boolean NOT NULL DEFAULT true,
  category    category_code NOT NULL,
  created_at  timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE phase_activities (
  id          bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  phase_id    bigint NOT NULL,
  name        varchar NOT NULL,
  description text,
  time_type   text,
  created_at  timestamptz NOT NULL DEFAULT now(),
  updated_at  timestamptz NOT NULL DEFAULT now(),

  CONSTRAINT fk_phase_activities_phase
    FOREIGN KEY (phase_id) REFERENCES phases(id)
    ON DELETE CASCADE
);

CREATE TABLE checklists (
  id          bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  name        varchar NOT NULL,
  description text,
  phase_id    bigint,
  created_at  timestamptz NOT NULL DEFAULT now(),
  updated_at  timestamptz NOT NULL DEFAULT now(),
  deleted_at  timestamptz,

  CONSTRAINT fk_checklists_phase
    FOREIGN KEY (phase_id) REFERENCES phases(id)
    ON DELETE SET NULL
);


-- DAILY CHECKLISTS
CREATE TABLE daily_checklists (
  id            bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  kandang_id    bigint NOT NULL,
  checklist_id  bigint NOT NULL,
  date          date NOT NULL,
  name          varchar,
  status        varchar,
  category      category_code NOT NULL,
  total_score   integer,
  document_path varchar,
  reject_reason text,
  created_by    bigint,
  created_at    timestamptz NOT NULL DEFAULT now(),
  updated_at    timestamptz NOT NULL DEFAULT now(),

  CONSTRAINT fk_daily_checklists_kandang
    FOREIGN KEY (kandang_id) REFERENCES kandangs(id)
    ON DELETE CASCADE,

  CONSTRAINT fk_daily_checklists_checklist
    FOREIGN KEY (checklist_id) REFERENCES checklists(id)
    ON DELETE RESTRICT,

  CONSTRAINT fk_daily_checklists_created_by
    FOREIGN KEY (created_by) REFERENCES users(id)
    ON DELETE SET NULL
);


--RELASI CHECKLIST ⇄ PHASE

CREATE TABLE daily_checklist_phases (
  id           bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  checklist_id bigint NOT NULL,
  phase_id     bigint NOT NULL,
  created_at   timestamptz NOT NULL DEFAULT now(),

  CONSTRAINT fk_dcp_checklist
    FOREIGN KEY (checklist_id) REFERENCES checklists(id)
    ON DELETE CASCADE,

  CONSTRAINT fk_dcp_phase
    FOREIGN KEY (phase_id) REFERENCES phases(id)
    ON DELETE CASCADE,

  CONSTRAINT uq_daily_checklist_phases UNIQUE (checklist_id, phase_id)
);


--ACTIVITY TASKS & ASSIGNMENT


CREATE TABLE daily_checklist_activity_tasks (
  id               bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  checklist_id     bigint NOT NULL,
  phase_id         bigint NOT NULL,
  phase_activity_id bigint NOT NULL,
  time_type        text,
  notes            text,
  created_at       timestamptz NOT NULL DEFAULT now(),
  updated_at       timestamptz NOT NULL DEFAULT now(),

  CONSTRAINT fk_dcat_checklist
    FOREIGN KEY (checklist_id) REFERENCES checklists(id)
    ON DELETE CASCADE,

  CONSTRAINT fk_dcat_phase
    FOREIGN KEY (phase_id) REFERENCES phases(id)
    ON DELETE CASCADE,

  CONSTRAINT fk_dcat_phase_activity
    FOREIGN KEY (phase_activity_id) REFERENCES phase_activities(id)
    ON DELETE CASCADE
);

CREATE TABLE daily_checklist_activity_task_assignments (
  id          bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  task_id     bigint NOT NULL,
  employee_id bigint NOT NULL,
  checked     boolean NOT NULL DEFAULT false,
  note        text,
  created_at  timestamptz NOT NULL DEFAULT now(),
  updated_at  timestamptz NOT NULL DEFAULT now(),

  CONSTRAINT fk_assignment_task
    FOREIGN KEY (task_id) REFERENCES daily_checklist_activity_tasks(id)
    ON DELETE CASCADE,

  CONSTRAINT fk_assignment_employee
    FOREIGN KEY (employee_id) REFERENCES employees(id)
    ON DELETE CASCADE
);

--DAILY CHECKLIST TASK RESULT
CREATE TABLE daily_checklist_tasks (
  id                bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  daily_checklist_id bigint NOT NULL,
  checklist_id      bigint NOT NULL,
  checklist_item_id bigint,
  is_completed      boolean NOT NULL DEFAULT false,
  score_value       integer,
  notes             text,
  photo_proof       varchar,
  status            varchar,
  created_at        timestamptz NOT NULL DEFAULT now(),
  updated_at        timestamptz NOT NULL DEFAULT now(),

  CONSTRAINT fk_dct_daily
    FOREIGN KEY (daily_checklist_id) REFERENCES daily_checklists(id)
    ON DELETE CASCADE,

  CONSTRAINT fk_dct_checklist
    FOREIGN KEY (checklist_id) REFERENCES checklists(id)
    ON DELETE CASCADE,

  CONSTRAINT fk_dct_checklist_item
    FOREIGN KEY (checklist_item_id) REFERENCES phase_activities(id)
    ON DELETE SET NULL
);
