-- 028: Disputes (admin compliance - plaintiff/defendant model)
-- Complements 021_create_disputes.sql with the updated entity schema used by admin controllers.
-- If 021 already created the disputes table, run ALTER or skip. This DDL is for fresh installs.

CREATE TABLE IF NOT EXISTS disputes (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    order_id UUID NOT NULL,
    plaintiff_id UUID NOT NULL REFERENCES users(id),
    defendant_id UUID REFERENCES users(id),
    type VARCHAR(30) NOT NULL CHECK (type IN ('buyer_claim', 'seller_claim', 'refund_request')),
    status VARCHAR(20) NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'processing', 'resolved', 'rejected')),
    amount NUMERIC(18, 2) NOT NULL DEFAULT 0,
    description TEXT,
    resolution TEXT,
    resolved_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    version INT NOT NULL DEFAULT 1
);

CREATE INDEX IF NOT EXISTS idx_disputes_status ON disputes(status);
CREATE INDEX IF NOT EXISTS idx_disputes_plaintiff_id ON disputes(plaintiff_id);
CREATE INDEX IF NOT EXISTS idx_disputes_defendant_id ON disputes(defendant_id);
CREATE INDEX IF NOT EXISTS idx_disputes_order_id ON disputes(order_id);
CREATE INDEX IF NOT EXISTS idx_disputes_created_at ON disputes(created_at DESC);
