-- 킨텍스 Phase B(§5B) — 공통 업무기능 모듈 스키마 (WISE/UIWS 이식, 멱등)
-- V1~V7 불변. worklog·schedule·message·notice·opinion·meeting·report·notification.
-- stats/search 는 별도 테이블 없이 아래 테이블을 집계/검색한다.

CREATE TABLE IF NOT EXISTS worklog (
    id          varchar(40)  PRIMARY KEY,
    event_id    varchar(40),
    writer_id   varchar(40)  NOT NULL,
    writer_name varchar(120),
    dept_id     varchar(40),
    work_date   date         NOT NULL,
    title       varchar(300) NOT NULL,
    work_type   varchar(20)  NOT NULL DEFAULT 'OPERATION',
    progress    varchar(10)  NOT NULL DEFAULT 'P0',
    status      varchar(20)  NOT NULL DEFAULT 'TODO',
    content     text,
    hours       numeric(5,2) NOT NULL DEFAULT 0,
    created_at  timestamptz  NOT NULL DEFAULT now(),
    updated_at  timestamptz  NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_worklog_writer_date ON worklog(writer_id, work_date DESC);
CREATE INDEX IF NOT EXISTS idx_worklog_event ON worklog(event_id, work_date DESC);

CREATE TABLE IF NOT EXISTS schedule (
    id            varchar(40)  PRIMARY KEY,
    event_id      varchar(40),
    owner_id      varchar(40)  NOT NULL,
    owner_name    varchar(120),
    dept_id       varchar(40),
    title         varchar(300) NOT NULL,
    schedule_type varchar(20)  NOT NULL DEFAULT 'PERSONAL',
    importance    varchar(20)  NOT NULL DEFAULT 'NORMAL',
    start_at      timestamptz  NOT NULL,
    end_at        timestamptz,
    all_day       boolean      NOT NULL DEFAULT false,
    location      varchar(200),
    content       text,
    color         varchar(20),
    created_at    timestamptz  NOT NULL DEFAULT now(),
    updated_at    timestamptz  NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_schedule_owner_start ON schedule(owner_id, start_at);
CREATE INDEX IF NOT EXISTS idx_schedule_event_start ON schedule(event_id, start_at);

CREATE TABLE IF NOT EXISTS message (
    id          varchar(40)  PRIMARY KEY,
    sender_id   varchar(40)  NOT NULL,
    sender_name varchar(120),
    title       varchar(300),
    content     text,
    created_at  timestamptz  NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS message_recipient (
    id           varchar(40) PRIMARY KEY,
    message_id   varchar(40) NOT NULL REFERENCES message(id) ON DELETE CASCADE,
    recipient_id varchar(40) NOT NULL,
    recv_type    varchar(10) NOT NULL DEFAULT 'RECV',
    read_at      timestamptz,
    deleted      boolean     NOT NULL DEFAULT false
);
CREATE INDEX IF NOT EXISTS idx_msg_recipient ON message_recipient(recipient_id, read_at);

CREATE TABLE IF NOT EXISTS notice (
    id           varchar(40)  PRIMARY KEY,
    event_id     varchar(40),
    category     varchar(20)  NOT NULL DEFAULT 'GENERAL',
    title        varchar(300) NOT NULL,
    content      text,
    pinned       boolean      NOT NULL DEFAULT false,
    author_id    varchar(40)  NOT NULL,
    author_name  varchar(120),
    view_count   integer      NOT NULL DEFAULT 0,
    published_at timestamptz,
    created_at   timestamptz  NOT NULL DEFAULT now(),
    updated_at   timestamptz  NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_notice_event ON notice(event_id, pinned DESC, created_at DESC);

CREATE TABLE IF NOT EXISTS opinion (
    id          varchar(40)  PRIMARY KEY,
    event_id    varchar(40),
    category    varchar(40),
    title       varchar(300) NOT NULL,
    content     text,
    status      varchar(20)  NOT NULL DEFAULT 'OPEN',
    author_id   varchar(40)  NOT NULL,
    author_name varchar(120),
    secret_yn   char(1)      NOT NULL DEFAULT 'N',
    created_at  timestamptz  NOT NULL DEFAULT now(),
    updated_at  timestamptz  NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_opinion_event ON opinion(event_id, created_at DESC);
CREATE TABLE IF NOT EXISTS opinion_comment (
    id          varchar(40) PRIMARY KEY,
    opinion_id  varchar(40) NOT NULL REFERENCES opinion(id) ON DELETE CASCADE,
    author_id   varchar(40) NOT NULL,
    author_name varchar(120),
    content     text        NOT NULL,
    created_at  timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_opinion_comment ON opinion_comment(opinion_id, created_at);

CREATE TABLE IF NOT EXISTS meeting (
    id           varchar(40)  PRIMARY KEY,
    event_id     varchar(40),
    title        varchar(300) NOT NULL,
    location     varchar(200),
    meeting_at   timestamptz  NOT NULL,
    organizer_id varchar(40)  NOT NULL,
    organizer_name varchar(120),
    content      text,
    minutes      text,
    created_at   timestamptz  NOT NULL DEFAULT now(),
    updated_at   timestamptz  NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_meeting_event ON meeting(event_id, meeting_at DESC);
CREATE TABLE IF NOT EXISTS meeting_action (
    id          varchar(40) PRIMARY KEY,
    meeting_id  varchar(40) NOT NULL REFERENCES meeting(id) ON DELETE CASCADE,
    seq         integer     NOT NULL DEFAULT 1,
    action_item varchar(500) NOT NULL,
    assignee_id varchar(40),
    due_date    date,
    status      varchar(20) NOT NULL DEFAULT 'TODO'
);
CREATE INDEX IF NOT EXISTS idx_meeting_action ON meeting_action(meeting_id, seq);

CREATE TABLE IF NOT EXISTS report (
    id          varchar(40)  PRIMARY KEY,
    event_id    varchar(40),
    report_type varchar(20)  NOT NULL DEFAULT 'DAILY',
    period_from date,
    period_to   date,
    title       varchar(300) NOT NULL,
    content     text,
    author_id   varchar(40)  NOT NULL,
    author_name varchar(120),
    created_at  timestamptz  NOT NULL DEFAULT now(),
    updated_at  timestamptz  NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_report_event ON report(event_id, created_at DESC);

CREATE TABLE IF NOT EXISTS notification (
    id           varchar(40)  PRIMARY KEY,
    recipient_id varchar(40)  NOT NULL,
    event_id     varchar(40),
    noti_type    varchar(20)  NOT NULL DEFAULT 'SYSTEM',
    title        varchar(300) NOT NULL,
    message      text,
    link         varchar(300),
    read_at      timestamptz,
    created_at   timestamptz  NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_notification_recipient ON notification(recipient_id, read_at, created_at DESC);
