CREATE TABLE IF NOT EXISTS payments (
    id BIGSERIAL PRIMARY KEY,
    payment_code VARCHAR(50) NOT NULL,
    reference_number VARCHAR(100) NULL,
    transaction_type VARCHAR(50),
    party_type VARCHAR(50) NOT NULL,
    party_id BIGINT NOT NULL,
    payment_date TIMESTAMPTZ NOT NULL,
    payment_method VARCHAR(20) NOT NULL,
    bank_id BIGINT NULL REFERENCES banks(id) ON DELETE RESTRICT ON UPDATE CASCADE,
    direction VARCHAR(5) NOT NULL,
    nominal NUMERIC(15, 3) NOT NULL,
    notes TEXT NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    deleted_at TIMESTAMPTZ NULL,
    created_by BIGINT REFERENCES users (id) ON DELETE SET NULL ON UPDATE CASCADE
);

-- Indexes
CREATE INDEX payments_party_polymorphic ON payments (party_type, party_id);
CREATE INDEX idx_payments_bank_id ON payments (bank_id);
