-- V67: 원천세 분기신고 테이블

CREATE TABLE withholding_tax_report (
    report_id       BIGSERIAL      PRIMARY KEY,
    settle_quarter  VARCHAR(7)     NOT NULL,
    agent_id        BIGINT         NOT NULL REFERENCES agent(agent_id),
    total_income    NUMERIC(18,2),
    total_withheld  NUMERIC(18,2),
    total_local_tax NUMERIC(18,2),
    generated_at    TIMESTAMP,
    filed_at        TIMESTAMP,
    file_status     VARCHAR(20)    NOT NULL DEFAULT 'DRAFT',
    created_at      TIMESTAMP      NOT NULL DEFAULT now(),
    updated_at      TIMESTAMP,
    UNIQUE (agent_id, settle_quarter),
    CONSTRAINT chk_wtr_status CHECK (file_status IN ('DRAFT','FILED','AMENDED'))
);

COMMENT ON TABLE withholding_tax_report IS '원천세 분기신고';
COMMENT ON COLUMN withholding_tax_report.settle_quarter IS '정산 분기 (예: 2025-Q1)';
COMMENT ON COLUMN withholding_tax_report.file_status    IS 'DRAFT=작성중, FILED=신고완료, AMENDED=수정신고';

CREATE INDEX idx_wtr_quarter ON withholding_tax_report(settle_quarter);
