-- V66: 회계 계정코드 마스터 신설
-- contract_accounting_entry.debit_account / credit_account 참조용
-- FK 추가는 PL 별도 논의 후 진행

CREATE TABLE account_code (
    account_code  VARCHAR(20)   PRIMARY KEY,
    account_name  VARCHAR(100)  NOT NULL,
    account_type  VARCHAR(10)   NOT NULL CHECK (account_type IN ('DEBIT','CREDIT','BOTH')),
    is_active     CHAR(1)       NOT NULL DEFAULT 'Y',
    sort_order    INTEGER       NOT NULL DEFAULT 0,
    description   VARCHAR(200),
    created_at    TIMESTAMP     NOT NULL DEFAULT now(),
    created_by    BIGINT,
    updated_at    TIMESTAMP     NOT NULL DEFAULT now(),
    updated_by    BIGINT
);

COMMENT ON TABLE account_code IS '회계 계정코드 마스터';
COMMENT ON COLUMN account_code.account_type IS 'DEBIT=차변전용, CREDIT=대변전용, BOTH=겸용';

INSERT INTO account_code (account_code, account_name, account_type, sort_order) VALUES
    ('8350', '지급수수료', 'DEBIT',  10),
    ('2530', '미지급금',   'CREDIT', 20),
    ('1130', '미수수수료', 'DEBIT',  30),
    ('4110', '수수료수익', 'CREDIT', 40)
ON CONFLICT (account_code) DO NOTHING;
