-- Script to seed mock orders for user test@mall.com
-- This guarantees data for: Pending Payment, Pending Shipment, Pending Receipt, Completed, Cancelled.

DO $$
DECLARE
    v_user_email TEXT := 'test@mall.com';
    v_user_id UUID;
    v_merchant_id UUID;
    v_product_id UUID;
    v_sku_id UUID;
    v_product_price DECIMAL;
    v_order_id UUID;
BEGIN
    -- 1. Get User
    SELECT id INTO v_user_id FROM public.ak_users WHERE email = v_user_email LIMIT 1;
    IF v_user_id IS NULL THEN RAISE NOTICE 'User not found'; RETURN; END IF;

    -- 2. Get Merchant & Product
    SELECT id, merchant_id, base_price INTO v_product_id, v_merchant_id, v_product_price 
    FROM public.ml_products WHERE status = 1 LIMIT 1;
    
    -- 3. Get SKU
    SELECT id INTO v_sku_id FROM public.ml_product_skus WHERE product_id = v_product_id LIMIT 1;

    -------------------------------------------------------
    -- 1. Pending Payment (Status 1)
    -------------------------------------------------------
    INSERT INTO public.ml_orders (
        order_no, user_id, merchant_id, product_amount, total_amount, shipping_address, 
        order_status, payment_status, shipping_status, created_at
    ) VALUES (
        'ORD_PENDING_' || substr(md5(random()::text), 1, 6), v_user_id, v_merchant_id, v_product_price, v_product_price + 10,
        '{"name": "Test User", "phone": "13800000000", "address": "Beijing China"}'::jsonb,
        1, 1, 1, NOW()
    ) RETURNING id INTO v_order_id;

    INSERT INTO public.ml_order_items (order_id, product_id, sku_id, product_name, price, quantity, total_amount)
    VALUES (v_order_id, v_product_id, v_sku_id, 'Pending Order Product', v_product_price, 1, v_product_price);

    -------------------------------------------------------
    -- 2. Pending Shipment (Status 2) - Paid
    -------------------------------------------------------
    INSERT INTO public.ml_orders (
        order_no, user_id, merchant_id, product_amount, total_amount, shipping_address, 
        order_status, payment_status, shipping_status, paid_at, created_at
    ) VALUES (
        'ORD_SHIP_' || substr(md5(random()::text), 1, 6), v_user_id, v_merchant_id, v_product_price * 2, v_product_price * 2 + 10,
        '{"name": "Test User", "phone": "13800000000", "address": "Beijing China"}'::jsonb,
        2, 2, 1, NOW(), NOW() - INTERVAL '1 hour'
    ) RETURNING id INTO v_order_id;

    INSERT INTO public.ml_order_items (order_id, product_id, sku_id, product_name, price, quantity, total_amount)
    VALUES (v_order_id, v_product_id, v_sku_id, 'To Ship Product', v_product_price, 2, v_product_price * 2);

    -------------------------------------------------------
    -- 3. Pending Receipt (Status 3) - Shipped
    -------------------------------------------------------
    INSERT INTO public.ml_orders (
        order_no, user_id, merchant_id, product_amount, total_amount, shipping_address, 
        order_status, payment_status, shipping_status, paid_at, shipped_at, created_at
    ) VALUES (
        'ORD_RCV_' || substr(md5(random()::text), 1, 6), v_user_id, v_merchant_id, v_product_price, v_product_price + 10,
        '{"name": "Test User", "phone": "13800000000", "address": "Beijing China"}'::jsonb,
        3, 2, 2, NOW() - INTERVAL '2 days', NOW() - INTERVAL '1 day', NOW() - INTERVAL '3 days'
    ) RETURNING id INTO v_order_id;

    INSERT INTO public.ml_order_items (order_id, product_id, sku_id, product_name, price, quantity, total_amount)
    VALUES (v_order_id, v_product_id, v_sku_id, 'Shipped Product', v_product_price, 1, v_product_price);

    -------------------------------------------------------
    -- 4. Completed (Status 4)
    -------------------------------------------------------
    INSERT INTO public.ml_orders (
        order_no, user_id, merchant_id, product_amount, total_amount, shipping_address, 
        order_status, payment_status, shipping_status, paid_at, shipped_at, delivered_at, completed_at, created_at
    ) VALUES (
        'ORD_DONE_' || substr(md5(random()::text), 1, 6), v_user_id, v_merchant_id, v_product_price, v_product_price,
        '{"name": "Test User", "phone": "13800000000", "address": "Beijing China"}'::jsonb,
        4, 2, 4, NOW() - INTERVAL '10 days', NOW() - INTERVAL '9 days', NOW() - INTERVAL '8 days', NOW() - INTERVAL '8 days', NOW() - INTERVAL '11 days'
    ) RETURNING id INTO v_order_id;

    INSERT INTO public.ml_order_items (order_id, product_id, sku_id, product_name, price, quantity, total_amount)
    VALUES (v_order_id, v_product_id, v_sku_id, 'Completed Product', v_product_price, 1, v_product_price);

    RAISE NOTICE 'Seeded orders for test user.';
END $$;
