-- 021: Dispute cases (compliance-service)
CREATE TABLE IF NOT EXISTS disputes (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    type VARCHAR(30) NOT NULL CHECK (type IN ('buyer_complaint', 'seller_complaint', 'refund_request')),
    status VARCHAR(20) NOT NULL DEFAULT 'submitted' CHECK (status IN (
        'submitted', 'evidence_collection', 'arbitration', 'resolved', 'escalated'
    )),
    buyer_id UUID NOT NULL REFERENCES users(id),
    seller_id UUID NOT NULL REFERENCES users(id),
    order_id UUID REFERENCES orders(id),
    coupon_id UUID REFERENCES coupons(id),
    description TEXT,
    evidence JSONB DEFAULT '[]',
    chain_evidence JSONB DEFAULT '[]',
    resolution TEXT,
    refund_approved BOOLEAN,
    sla_deadline TIMESTAMPTZ,
    resolved_by UUID REFERENCES users(id),
    resolved_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_disputes_status ON disputes(status);
CREATE INDEX idx_disputes_buyer_id ON disputes(buyer_id);
CREATE INDEX idx_disputes_seller_id ON disputes(seller_id);
