-- ============================================================================
-- LeAudit Platform — 交叉评查第一阶段表结构
-- ============================================================================

BEGIN;

CREATE TABLE IF NOT EXISTS leaudit_cross_review_tasks (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    task_name VARCHAR(255) NOT NULL,
    task_type VARCHAR(32) NOT NULL,
    doc_type_id BIGINT,
    doc_type_code VARCHAR(64),
    assigner_id BIGINT NOT NULL,
    status VARCHAR(32) NOT NULL DEFAULT 'in_progress',
    create_time TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    update_time TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    delete_time TIMESTAMPTZ
);

CREATE INDEX IF NOT EXISTS idx_lcr_tasks_assigner_id ON leaudit_cross_review_tasks (assigner_id);
CREATE INDEX IF NOT EXISTS idx_lcr_tasks_status ON leaudit_cross_review_tasks (status);
CREATE INDEX IF NOT EXISTS idx_lcr_tasks_doc_type_id ON leaudit_cross_review_tasks (doc_type_id);

COMMENT ON TABLE leaudit_cross_review_tasks IS '交叉评查任务主表';

CREATE TABLE IF NOT EXISTS leaudit_cross_review_task_members (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    task_id BIGINT NOT NULL,
    user_id BIGINT NOT NULL,
    member_role VARCHAR(32) NOT NULL DEFAULT 'participant',
    create_time TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    update_time TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    delete_time TIMESTAMPTZ
);

CREATE INDEX IF NOT EXISTS idx_lcr_task_members_task_id ON leaudit_cross_review_task_members (task_id);
CREATE INDEX IF NOT EXISTS idx_lcr_task_members_user_id ON leaudit_cross_review_task_members (user_id);
CREATE INDEX IF NOT EXISTS idx_lcr_task_members_role ON leaudit_cross_review_task_members (member_role);
CREATE UNIQUE INDEX IF NOT EXISTS uq_lcr_task_members_task_user_active
    ON leaudit_cross_review_task_members (task_id, user_id)
    WHERE delete_time IS NULL;

COMMENT ON TABLE leaudit_cross_review_task_members IS '交叉评查任务成员表';

CREATE TABLE IF NOT EXISTS leaudit_cross_review_task_documents (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    task_id BIGINT NOT NULL,
    document_id BIGINT NOT NULL,
    audit_status INTEGER NOT NULL DEFAULT 0,
    create_time TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    update_time TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    delete_time TIMESTAMPTZ
);

CREATE INDEX IF NOT EXISTS idx_lcr_task_documents_task_id ON leaudit_cross_review_task_documents (task_id);
CREATE INDEX IF NOT EXISTS idx_lcr_task_documents_document_id ON leaudit_cross_review_task_documents (document_id);
CREATE INDEX IF NOT EXISTS idx_lcr_task_documents_task_status ON leaudit_cross_review_task_documents (task_id, audit_status);
CREATE UNIQUE INDEX IF NOT EXISTS uq_lcr_task_documents_task_document_active
    ON leaudit_cross_review_task_documents (task_id, document_id)
    WHERE delete_time IS NULL;

COMMENT ON TABLE leaudit_cross_review_task_documents IS '交叉评查任务文档挂载表';

CREATE TABLE IF NOT EXISTS leaudit_cross_review_proposals (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    task_id BIGINT NOT NULL,
    document_id BIGINT NOT NULL,
    rule_result_id BIGINT NOT NULL,
    proposer_id BIGINT NOT NULL,
    proposed_score_delta NUMERIC(10, 2) NOT NULL,
    reason TEXT NOT NULL,
    status VARCHAR(32) NOT NULL DEFAULT 'pending',
    create_time TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    update_time TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    delete_time TIMESTAMPTZ
);

CREATE INDEX IF NOT EXISTS idx_lcr_proposals_task_id ON leaudit_cross_review_proposals (task_id);
CREATE INDEX IF NOT EXISTS idx_lcr_proposals_document_id ON leaudit_cross_review_proposals (document_id);
CREATE INDEX IF NOT EXISTS idx_lcr_proposals_rule_result_id ON leaudit_cross_review_proposals (rule_result_id);
CREATE INDEX IF NOT EXISTS idx_lcr_proposals_proposer_id ON leaudit_cross_review_proposals (proposer_id);
CREATE INDEX IF NOT EXISTS idx_lcr_proposals_status ON leaudit_cross_review_proposals (status);

COMMENT ON TABLE leaudit_cross_review_proposals IS '交叉评查提案表';

CREATE TABLE IF NOT EXISTS leaudit_cross_review_votes (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    proposal_id BIGINT NOT NULL,
    voter_id BIGINT NOT NULL,
    vote_type VARCHAR(16) NOT NULL,
    create_time TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    update_time TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    delete_time TIMESTAMPTZ
);

CREATE INDEX IF NOT EXISTS idx_lcr_votes_proposal_id ON leaudit_cross_review_votes (proposal_id);
CREATE INDEX IF NOT EXISTS idx_lcr_votes_voter_id ON leaudit_cross_review_votes (voter_id);
CREATE UNIQUE INDEX IF NOT EXISTS uq_lcr_votes_proposal_voter_active
    ON leaudit_cross_review_votes (proposal_id, voter_id)
    WHERE delete_time IS NULL;

COMMENT ON TABLE leaudit_cross_review_votes IS '交叉评查投票表';

COMMIT;

-- Add review_scope column
ALTER TABLE leaudit_documents ADD COLUMN IF NOT EXISTS review_scope VARCHAR(32) DEFAULT 'standard';
COMMENT ON COLUMN leaudit_documents.review_scope IS '评查范围: standard=个人评查 cross_review=交叉评查';

