-- =====================================================================
-- H2 인메모리 테스트 전용 스키마 (H2 2.x, MODE=PostgreSQL)
-- BIGSERIAL 대신 BIGINT GENERATED BY DEFAULT AS IDENTITY 사용
-- =====================================================================

CREATE TABLE IF NOT EXISTS users (
    id                  BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    email               VARCHAR(255) NOT NULL UNIQUE,
    password            VARCHAR(255) NOT NULL,
    name                VARCHAR(100),
    role                VARCHAR(50)  NOT NULL DEFAULT 'USER',
    last_login_at       TIMESTAMP,
    created_at          TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
    failed_attempt      INT          NOT NULL DEFAULT 0,
    account_locked      BOOLEAN      NOT NULL DEFAULT FALSE,
    is_out              BOOLEAN      NOT NULL DEFAULT FALSE,
    deleted_at          TIMESTAMP,
    password_changed_at TIMESTAMP,
    oauth_provider      VARCHAR(30),
    oauth_id            VARCHAR(100),
    otp_code            VARCHAR(10),
    otp_expires_at      TIMESTAMP,
    two_factor_enabled  BOOLEAN      NOT NULL DEFAULT FALSE,
    user_id             VARCHAR(20),
    dept_code           VARCHAR(10),
    user_type           CHAR(2)      NOT NULL DEFAULT '01',
    corp_name           VARCHAR(100),
    auth_level          INT          NOT NULL DEFAULT 1,
    access_ip           VARCHAR(15),
    use_yn              CHAR(1)      NOT NULL DEFAULT 'Y',
    phone               VARCHAR(20),
    department          VARCHAR(100),
    retire_dt           DATE,
    zip_code            VARCHAR(10),
    address             VARCHAR(200),
    address_detail      VARCHAR(100)
);

CREATE TABLE IF NOT EXISTS login_history (
    id          BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    user_email  VARCHAR(255) NOT NULL,
    login_at    TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
    ip_address  VARCHAR(50),
    user_agent  VARCHAR(512),
    status      VARCHAR(20)  NOT NULL
);

CREATE TABLE IF NOT EXISTS activity_logs (
    id          BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    user_email  VARCHAR(255) NOT NULL,
    action      VARCHAR(100) NOT NULL,
    target      VARCHAR(512),
    timestamp   TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS notifications (
    id                BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    message           TEXT         NOT NULL,
    target_role       VARCHAR(50),
    target_user_email VARCHAR(255),
    sent_at           TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
    is_read           BOOLEAN      NOT NULL DEFAULT FALSE
);

CREATE TABLE IF NOT EXISTS todos (
    id           BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    user_email   VARCHAR(255) NOT NULL,
    task         TEXT         NOT NULL,
    is_completed BOOLEAN      NOT NULL DEFAULT FALSE,
    priority     VARCHAR(20),
    due_date     DATE,
    created_at   TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS approval_requests (
    id               BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    title            VARCHAR(255) NOT NULL,
    content          TEXT,
    requester_email  VARCHAR(255) NOT NULL,
    status           VARCHAR(30)  NOT NULL DEFAULT 'PENDING',
    current_step     INT          NOT NULL DEFAULT 1,
    created_at       TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at       TIMESTAMP
);

CREATE TABLE IF NOT EXISTS approval_steps (
    id             BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    request_id     BIGINT       NOT NULL REFERENCES approval_requests(id) ON DELETE CASCADE,
    step_order     INT          NOT NULL,
    approver_email VARCHAR(255) NOT NULL,
    status         VARCHAR(30)  NOT NULL DEFAULT 'PENDING',
    comment        TEXT,
    processed_at   TIMESTAMP
);

CREATE TABLE IF NOT EXISTS notices (
    id            BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    title         VARCHAR(255) NOT NULL,
    content       TEXT         NOT NULL,
    author_email  VARCHAR(255) NOT NULL,
    category      VARCHAR(50)  NOT NULL DEFAULT 'GENERAL',
    is_important  BOOLEAN      NOT NULL DEFAULT FALSE,
    is_published  BOOLEAN      NOT NULL DEFAULT TRUE,
    view_count    INT          NOT NULL DEFAULT 0,
    created_at    TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at    TIMESTAMP
);

CREATE TABLE IF NOT EXISTS faq (
    id            BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    question      TEXT         NOT NULL,
    answer        TEXT         NOT NULL,
    category      VARCHAR(50)  NOT NULL DEFAULT 'GENERAL',
    sort_order    INT          NOT NULL DEFAULT 0,
    is_published  BOOLEAN      NOT NULL DEFAULT TRUE,
    created_at    TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at    TIMESTAMP
);

CREATE TABLE IF NOT EXISTS contact_us (
    id            BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    name          VARCHAR(100) NOT NULL,
    email         VARCHAR(255) NOT NULL,
    subject       VARCHAR(255) NOT NULL,
    message       TEXT         NOT NULL,
    status        VARCHAR(20)  NOT NULL DEFAULT 'PENDING',
    reply         TEXT,
    replied_at    TIMESTAMP,
    replied_by    VARCHAR(255),
    created_at    TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS groups (
    id          BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    group_code  VARCHAR(50)  NOT NULL UNIQUE,
    group_name  VARCHAR(100) NOT NULL,
    description TEXT,
    is_active   BOOLEAN      NOT NULL DEFAULT TRUE,
    sort_order  INT          NOT NULL DEFAULT 0,
    created_at  TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at  TIMESTAMP
);

CREATE TABLE IF NOT EXISTS user_groups (
    id         BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    user_id    BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    group_id   BIGINT NOT NULL REFERENCES groups(id) ON DELETE CASCADE,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(user_id, group_id)
);

CREATE TABLE IF NOT EXISTS sys_menus (
    id          BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    parent_id   BIGINT REFERENCES sys_menus(id) ON DELETE SET NULL,
    menu_code   VARCHAR(50)  NOT NULL UNIQUE,
    menu_name   VARCHAR(100) NOT NULL,
    menu_path   VARCHAR(200),
    icon        VARCHAR(100),
    sort_order  INT          NOT NULL DEFAULT 0,
    menu_level  INT          NOT NULL DEFAULT 1,
    is_active   BOOLEAN      NOT NULL DEFAULT TRUE,
    created_at  TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at  TIMESTAMP
);

CREATE TABLE IF NOT EXISTS group_menu_perms (
    id         BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    group_id   BIGINT NOT NULL REFERENCES groups(id) ON DELETE CASCADE,
    menu_id    BIGINT NOT NULL REFERENCES sys_menus(id) ON DELETE CASCADE,
    can_read   BOOLEAN NOT NULL DEFAULT FALSE,
    can_write  BOOLEAN NOT NULL DEFAULT FALSE,
    can_delete BOOLEAN NOT NULL DEFAULT FALSE,
    UNIQUE(group_id, menu_id)
);

CREATE TABLE IF NOT EXISTS common_code_groups (
    id          BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    group_code  VARCHAR(50)  NOT NULL UNIQUE,
    group_name  VARCHAR(100) NOT NULL,
    description TEXT,
    is_active   BOOLEAN      NOT NULL DEFAULT TRUE,
    sort_order  INT          NOT NULL DEFAULT 0,
    created_at  TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at  TIMESTAMP
);

CREATE TABLE IF NOT EXISTS common_codes (
    id          BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    group_id    BIGINT       NOT NULL REFERENCES common_code_groups(id) ON DELETE CASCADE,
    code        VARCHAR(50)  NOT NULL,
    code_name   VARCHAR(100) NOT NULL,
    description TEXT,
    sort_order  INT          NOT NULL DEFAULT 0,
    is_active   BOOLEAN      NOT NULL DEFAULT TRUE,
    created_at  TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at  TIMESTAMP,
    UNIQUE(group_id, code)
);

CREATE TABLE IF NOT EXISTS dept (
    dept_code    VARCHAR(10)  NOT NULL PRIMARY KEY,
    dept_name    VARCHAR(100) NOT NULL,
    parent_code  VARCHAR(10)  REFERENCES dept(dept_code),
    depth        INT          NOT NULL DEFAULT 1,
    sort_order   INT          NOT NULL DEFAULT 0,
    use_yn       CHAR(1)      NOT NULL DEFAULT 'Y',
    created_at   TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP
);
