-- create_pending_orders.sql
-- 在 dev 环境幂等创建若干 "待接取" 订单及对应配送任务
-- 使用说明：在 Supabase SQL Editor 中选择 Role = postgres，整体执行此文件。

-- 订单 PENDING-20260202-001
WITH o AS (
  INSERT INTO public.ml_orders (order_no, user_id, merchant_id, product_amount, shipping_fee, total_amount, shipping_address, order_status, created_at, updated_at)
  SELECT 'PENDING-20260202-001',
    COALESCE((SELECT id FROM public.ak_users WHERE email='zhang.san@example.com' LIMIT 1),(SELECT id FROM public.ak_users LIMIT 1)),
    (SELECT id FROM public.ak_users WHERE email='merchant.real@example.com' LIMIT 1),
    12.50, 3.00, 15.50,
    ('{"contact":"张三","phone":"13811112222","detail":"测试地址 A"}')::jsonb,
    1, NOW(), NOW()
  WHERE NOT EXISTS (SELECT 1 FROM public.ml_orders WHERE order_no='PENDING-20260202-001')
  RETURNING id
), it AS (
  INSERT INTO public.ml_order_items (id, order_id, product_id, product_name, price, quantity, total_amount, created_at)
  SELECT uuid_generate_v4(), (SELECT id FROM o),
    COALESCE((SELECT id FROM public.ml_products WHERE product_code='REAL-P-A-20260202' LIMIT 1),(SELECT id FROM public.ml_products LIMIT 1)),
    COALESCE((SELECT name FROM public.ml_products WHERE product_code='REAL-P-A-20260202' LIMIT 1),'商品A'),
    COALESCE((SELECT base_price FROM public.ml_products WHERE product_code='REAL-P-A-20260202' LIMIT 1),12.50),1,
    COALESCE((SELECT base_price FROM public.ml_products WHERE product_code='REAL-P-A-20260202' LIMIT 1),12.50), NOW()
  WHERE EXISTS (SELECT 1 FROM o)
  RETURNING id
), t AS (
  INSERT INTO public.ml_delivery_tasks (order_id, pickup_address, delivery_address, delivery_fee, status, created_at, updated_at)
  SELECT (SELECT id FROM o),
    ('{"contact":"门店A","phone":"13900000001","detail":"门店A"}')::jsonb,
    ('{"contact":"张三","phone":"13811112222","detail":"测试地址 A"}')::jsonb,
    3.00, 1, NOW(), NOW()
  WHERE EXISTS (SELECT 1 FROM o)
  RETURNING id
)
SELECT 'PENDING-20260202-001' AS order_no, (SELECT id FROM o) AS order_id, (SELECT id FROM t) AS task_id;

-- 订单 PENDING-20260202-002
WITH o AS (
  INSERT INTO public.ml_orders (order_no, user_id, merchant_id, product_amount, shipping_fee, total_amount, shipping_address, order_status, created_at, updated_at)
  SELECT 'PENDING-20260202-002',
    COALESCE((SELECT id FROM public.ak_users WHERE email='li.si@example.com' LIMIT 1),(SELECT id FROM public.ak_users LIMIT 1)),
    (SELECT id FROM public.ak_users WHERE email='merchant.real@example.com' LIMIT 1),
    35.00, 4.00, 39.00,
    ('{"contact":"李四","phone":"13822223333","detail":"测试地址 B"}')::jsonb,
    1, NOW(), NOW()
  WHERE NOT EXISTS (SELECT 1 FROM public.ml_orders WHERE order_no='PENDING-20260202-002')
  RETURNING id
), it AS (
  INSERT INTO public.ml_order_items (id, order_id, product_id, product_name, price, quantity, total_amount, created_at)
  SELECT uuid_generate_v4(), (SELECT id FROM o),
    COALESCE((SELECT id FROM public.ml_products WHERE product_code='REAL-P-B-20260202' LIMIT 1),(SELECT id FROM public.ml_products LIMIT 1)),
    COALESCE((SELECT name FROM public.ml_products WHERE product_code='REAL-P-B-20260202' LIMIT 1),'商品B'),
    COALESCE((SELECT base_price FROM public.ml_products WHERE product_code='REAL-P-B-20260202' LIMIT 1),35.00),1,
    COALESCE((SELECT base_price FROM public.ml_products WHERE product_code='REAL-P-B-20260202' LIMIT 1),35.00), NOW()
  WHERE EXISTS (SELECT 1 FROM o)
  RETURNING id
), t AS (
  INSERT INTO public.ml_delivery_tasks (order_id, pickup_address, delivery_address, delivery_fee, status, created_at, updated_at)
  SELECT (SELECT id FROM o),
    ('{"contact":"门店B","phone":"13900000002","detail":"门店B"}')::jsonb,
    ('{"contact":"李四","phone":"13822223333","detail":"测试地址 B"}')::jsonb,
    4.00, 1, NOW(), NOW()
  WHERE EXISTS (SELECT 1 FROM o)
  RETURNING id
)
SELECT 'PENDING-20260202-002' AS order_no, (SELECT id FROM o) AS order_id, (SELECT id FROM t) AS task_id;

-- 订单 PENDING-20260202-003
WITH o AS (
  INSERT INTO public.ml_orders (order_no, user_id, merchant_id, product_amount, shipping_fee, total_amount, shipping_address, order_status, created_at, updated_at)
  SELECT 'PENDING-20260202-003',
    COALESCE((SELECT id FROM public.ak_users WHERE email='wang.wu@example.com' LIMIT 1),(SELECT id FROM public.ak_users LIMIT 1)),
    (SELECT id FROM public.ak_users WHERE email='merchant.real@example.com' LIMIT 1),
    22.00, 3.50, 25.50,
    ('{"contact":"王五","phone":"13833334444","detail":"测试地址 C"}')::jsonb,
    1, NOW(), NOW()
  WHERE NOT EXISTS (SELECT 1 FROM public.ml_orders WHERE order_no='PENDING-20260202-003')
  RETURNING id
), it AS (
  INSERT INTO public.ml_order_items (id, order_id, product_id, product_name, price, quantity, total_amount, created_at)
  SELECT uuid_generate_v4(), (SELECT id FROM o),
    COALESCE((SELECT id FROM public.ml_products WHERE product_code='REAL-P-C-20260202' LIMIT 1),(SELECT id FROM public.ml_products LIMIT 1)),
    COALESCE((SELECT name FROM public.ml_products WHERE product_code='REAL-P-C-20260202' LIMIT 1),'商品C'),
    COALESCE((SELECT base_price FROM public.ml_products WHERE product_code='REAL-P-C-20260202' LIMIT 1),22.00),1,
    COALESCE((SELECT base_price FROM public.ml_products WHERE product_code='REAL-P-C-20260202' LIMIT 1),22.00), NOW()
  WHERE EXISTS (SELECT 1 FROM o)
  RETURNING id
), t AS (
  INSERT INTO public.ml_delivery_tasks (order_id, pickup_address, delivery_address, delivery_fee, status, created_at, updated_at)
  SELECT (SELECT id FROM o),
    ('{"contact":"门店C","phone":"13900000003","detail":"门店C"}')::jsonb,
    ('{"contact":"王五","phone":"13833334444","detail":"测试地址 C"}')::jsonb,
    3.50, 1, NOW(), NOW()
  WHERE EXISTS (SELECT 1 FROM o)
  RETURNING id
)
SELECT 'PENDING-20260202-003' AS order_no, (SELECT id FROM o) AS order_id, (SELECT id FROM t) AS task_id;

-- 最终：列出新建或已存在的 pending 订单
SELECT order_no, order_id, task_id FROM (
  VALUES
    ('PENDING-20260202-001',(SELECT id FROM public.ml_orders WHERE order_no='PENDING-20260202-001' LIMIT 1),(SELECT id FROM public.ml_delivery_tasks dt JOIN public.ml_orders o ON dt.order_id=o.id WHERE o.order_no='PENDING-20260202-001' LIMIT 1)),
    ('PENDING-20260202-002',(SELECT id FROM public.ml_orders WHERE order_no='PENDING-20260202-002' LIMIT 1),(SELECT id FROM public.ml_delivery_tasks dt JOIN public.ml_orders o ON dt.order_id=o.id WHERE o.order_no='PENDING-20260202-002' LIMIT 1)),
    ('PENDING-20260202-003',(SELECT id FROM public.ml_orders WHERE order_no='PENDING-20260202-003' LIMIT 1),(SELECT id FROM public.ml_delivery_tasks dt JOIN public.ml_orders o ON dt.order_id=o.id WHERE o.order_no='PENDING-20260202-003' LIMIT 1))
) AS t(order_no, order_id, task_id);
