-- Script to seed mock reviews for user test@mall.com
-- This script ensures necessary dependencies (Orders, Order Items) exist before adding reviews.

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_order_id UUID;
    v_order_item_id UUID;
    v_sku_code TEXT;
BEGIN
    -- 1. Get User ID from ak_users (ensure test user exists)
    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. Skipping review generation.', v_user_email;
        RETURN;
    END IF;

    -- 2. Find a valid Product and Merchant to attach reviews to
    -- We select the first active product
    SELECT id, merchant_id INTO v_product_id, v_merchant_id 
    FROM public.ml_products 
    WHERE status = 1 
    LIMIT 1;

    IF v_product_id IS NULL THEN
        RAISE NOTICE 'No active products found in database. Cannot create reviews.';
        RETURN;
    END IF;

    -- Get a valid SKU for this product (optional, but good for data integrity)
    SELECT id, sku_code INTO v_sku_id, v_sku_code 
    FROM public.ml_product_skus 
    WHERE product_id = v_product_id 
    LIMIT 1;

    RAISE NOTICE 'Found User: %, Product: %, Merchant: %', v_user_id, v_product_id, v_merchant_id;

    -------------------------------------------------------
    -- Review 1: 5-Star Review with Images
    -------------------------------------------------------
    
    -- Create a completed order for Review 1
    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 (
        'MOCK_REV_' || substr(md5(random()::text), 1, 10), -- Random Order No
        v_user_id, 
        v_merchant_id,
        199.00, 199.00,
        '{"name": "测试用户", "phone": "13800138000", "address": "虚拟测试地址"}'::jsonb,
        4, 2, 4, -- Completed, Paid, Delivered
        NOW() - INTERVAL '5 days',
        NOW() - INTERVAL '4 days',
        NOW() - INTERVAL '3 days',
        NOW() - INTERVAL '3 days',
        NOW() - INTERVAL '5 days'
    ) RETURNING id INTO v_order_id;

    -- Create Order Item for Review 1
    INSERT INTO public.ml_order_items (
        order_id, product_id, sku_id, 
        product_name, sku_name, price, quantity, total_amount
    ) VALUES (
        v_order_id, v_product_id, v_sku_id,
        '测试商品 (Mock Product)', '默认规格', 199.00, 1, 199.00
    ) RETURNING id INTO v_order_item_id;

    -- Insert Review 1
    INSERT INTO public.ml_product_reviews (
        user_id, order_id, order_item_id, product_id, merchant_id,
        rating, content, images, is_anonymous, status, created_at
    ) VALUES (
        v_user_id, v_order_id, v_order_item_id, v_product_id, v_merchant_id,
        5,
        '非常满意的购物体验！商品质量超乎想象，包装也很精美。发货速度快，客服态度好，下次还会再来购买！',
        '["https://picsum.photos/200/200", "https://picsum.photos/200/201", "https://picsum.photos/200/202"]'::jsonb,
        FALSE,
        1,
        NOW() - INTERVAL '2 days'
    );

    -------------------------------------------------------
    -- Review 2: 4-Star Anonymous Review
    -------------------------------------------------------

    -- Create a completed order for Review 2
    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 (
        'MOCK_REV_' || substr(md5(random()::text), 1, 10),
        v_user_id, 
        v_merchant_id,
        99.00, 99.00,
        '{"name": "测试用户", "phone": "13800138000", "address": "虚拟测试地址"}'::jsonb,
        4, 2, 4,
        NOW() - INTERVAL '10 days',
        NOW() - INTERVAL '9 days',
        NOW() - INTERVAL '8 days',
        NOW() - INTERVAL '8 days',
        NOW() - INTERVAL '10 days'
    ) RETURNING id INTO v_order_id;

    -- Create Order Item for Review 2
    INSERT INTO public.ml_order_items (
        order_id, product_id, sku_id, 
        product_name, sku_name, price, quantity, total_amount
    ) VALUES (
        v_order_id, v_product_id, v_sku_id,
        '测试商品 (Mock Product)', '默认规格', 99.00, 1, 99.00
    ) RETURNING id INTO v_order_item_id;

    -- Insert Review 2
    INSERT INTO public.ml_product_reviews (
        user_id, order_id, order_item_id, product_id, merchant_id,
        rating, content, images, is_anonymous, status, created_at
    ) VALUES (
        v_user_id, v_order_id, v_order_item_id, v_product_id, v_merchant_id,
        4,
        '整体还不错，性价比很高。就是物流稍微慢了一点点，不过不影响使用。好评！',
        '[]'::jsonb,
        TRUE, -- Anonymous
        1,
        NOW() - INTERVAL '7 days'
    );

    RAISE NOTICE 'Successfully added 2 mock reviews for user %', v_user_email;

END $$;
