-- Create Refund Table
CREATE TABLE public.ml_refunds (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    refund_no VARCHAR(50) UNIQUE NOT NULL,
    user_id UUID NOT NULL REFERENCES public.ak_users(id),
    order_id UUID NOT NULL REFERENCES public.ml_orders(id),
    refund_type INTEGER NOT NULL, -- 1: Money Only, 2: Return & Refund
    refund_reason VARCHAR(200) NOT NULL,
    refund_amount DECIMAL(12,2) NOT NULL,
    description TEXT,
    images JSONB DEFAULT '[]',
    status INTEGER DEFAULT 1, -- 1: Pending, 2: Approved, 3: Rejected, 4: Completed, 5: Cancelled
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

COMMENT ON TABLE public.ml_refunds IS '售后退款申请表';

-- Add RLS Policies (Optional but good practice)
ALTER TABLE public.ml_refunds ENABLE ROW LEVEL SECURITY;

CREATE POLICY "Users can view their own refunds"
ON public.ml_refunds FOR SELECT
USING (auth.uid() = user_id);

CREATE POLICY "Users can insert their own refunds"
ON public.ml_refunds FOR INSERT
WITH CHECK (auth.uid() = user_id);
