BEGIN;

DELETE FROM "DriverLocation" WHERE "id" IN (
  'seed-driver-location-delivered',
  'seed-driver-location-ontheway',
  'seed-driver-location-pickup'
);

DELETE FROM "Transaction" WHERE "id" IN (
  'seed-transaction-delivered',
  'seed-transaction-ontheway',
  'seed-transaction-pickup'
);

DELETE FROM "DeliveryRoute" WHERE "id" IN (
  'seed-route-delivered',
  'seed-route-ontheway',
  'seed-route-pickup'
);

DELETE FROM "Delivery" WHERE "id" IN (
  'seed-delivery-delivered',
  'seed-delivery-ontheway',
  'seed-delivery-pickup'
);

DELETE FROM "Payment" WHERE "id" IN (
  'seed-payment-delivered',
  'seed-payment-ontheway',
  'seed-payment-pickup'
);

DELETE FROM "OrderItem" WHERE "id" IN (
  'seed-order-item-delivered-1',
  'seed-order-item-delivered-2',
  'seed-order-item-ontheway-1',
  'seed-order-item-ontheway-2',
  'seed-order-item-pickup-1'
);

DELETE FROM "Order" WHERE "id" IN (
  'seed-order-delivered',
  'seed-order-ontheway',
  'seed-order-pickup'
);

DELETE FROM "ProductAdditional" WHERE "id" IN (
  'seed-additional-pizza',
  'seed-additional-burger',
  'seed-additional-combo'
);

DELETE FROM "ProductVariation" WHERE "id" IN (
  'seed-variation-pizza',
  'seed-variation-burger',
  'seed-variation-combo'
);

DELETE FROM "Product" WHERE "id" IN (
  'seed-product-pizza',
  'seed-product-burger',
  'seed-product-combo'
);

DELETE FROM "ProductCategory" WHERE "id" IN (
  'seed-category-pizzas',
  'seed-category-burgers',
  'seed-category-combos'
);

DELETE FROM "RestaurantCategory" WHERE "id" IN (
  'seed-restaurant-category-pizza',
  'seed-restaurant-category-burger',
  'seed-restaurant-category-combo'
);

DELETE FROM "Address" WHERE "id" = 'seed-address-customer';
DELETE FROM "Wallet" WHERE "id" IN (
  'seed-wallet-platform',
  'seed-wallet-restaurant',
  'seed-wallet-driver',
  'seed-wallet-customer'
);
DELETE FROM "Customer" WHERE "id" = 'seed-customer';
DELETE FROM "Driver" WHERE "id" = 'seed-driver';
DELETE FROM "Restaurant" WHERE "id" = 'seed-restaurant';

INSERT INTO "Restaurant" (
  "id", "userId", "name", "slug", "description", "documentNumber", "commissionPercentage", "serviceFeePercentage",
  "baseDeliveryFee", "pricePerKm", "latitude", "longitude", "city", "neighborhood", "isOpen", "isPaused", "status",
  "createdAt", "updatedAt"
) VALUES (
  'seed-restaurant', 'seed-user-restaurant', 'Forno da Esquina', 'forno-da-esquina',
  'Restaurante demo para validar cardapio publico, pedidos e operacao da loja.',
  '12345678000199', 12.5, 0, 5, 1.5, -23.55052, -46.633308, 'Sao Paulo', 'Centro', TRUE, FALSE, 'ACTIVE', NOW(), NOW()
);

INSERT INTO "Driver" (
  "id", "userId", "fullName", "rating", "documentNumber", "vehiclePlate", "vehicleModel", "pixKey", "status", "isOnline", "createdAt", "updatedAt"
) VALUES (
  'seed-driver', 'seed-user-driver', 'Mateus Entregas', 5, '12345678900', 'CHE1G01', 'Honda CG 160', 'entregador@chego.app', 'ACTIVE', TRUE, NOW(), NOW()
);

INSERT INTO "Customer" (
  "id", "userId", "fullName", "createdAt", "updatedAt"
) VALUES (
  'seed-customer', 'seed-user-customer', 'Camila Cliente', NOW(), NOW()
);

INSERT INTO "Wallet" (
  "id", "userId", "customerId", "driverId", "restaurantId", "type", "balance", "blockedBalance", "currency", "createdAt", "updatedAt"
) VALUES
  ('seed-wallet-platform', 'seed-user-admin', NULL, NULL, NULL, 'PLATFORM', 0, 0, 'BRL', NOW(), NOW()),
  ('seed-wallet-restaurant', 'seed-user-restaurant', NULL, NULL, 'seed-restaurant', 'RESTAURANT', 0, 0, 'BRL', NOW(), NOW()),
  ('seed-wallet-driver', 'seed-user-driver', NULL, 'seed-driver', NULL, 'DRIVER', 0, 0, 'BRL', NOW(), NOW()),
  ('seed-wallet-customer', 'seed-user-customer', 'seed-customer', NULL, NULL, 'CUSTOMER', 0, 0, 'BRL', NOW(), NOW());

INSERT INTO "RestaurantCategory" (
  "id", "restaurantId", "name", "description", "createdAt", "updatedAt"
) VALUES
  ('seed-restaurant-category-pizza', 'seed-restaurant', 'Pizza artesanal', 'Massas de longa fermentacao e ingredientes frescos.', NOW(), NOW()),
  ('seed-restaurant-category-burger', 'seed-restaurant', 'Burger premium', 'Lanches de alto giro para entrega rapida.', NOW(), NOW()),
  ('seed-restaurant-category-combo', 'seed-restaurant', 'Combos do dia', 'Ofertas para aumentar conversao no pedido publico.', NOW(), NOW());

INSERT INTO "ProductCategory" (
  "id", "restaurantId", "name", "sortOrder", "createdAt", "updatedAt"
) VALUES
  ('seed-category-pizzas', 'seed-restaurant', 'Pizzas', 1, NOW(), NOW()),
  ('seed-category-burgers', 'seed-restaurant', 'Burgers', 2, NOW(), NOW()),
  ('seed-category-combos', 'seed-restaurant', 'Combos', 3, NOW(), NOW());

INSERT INTO "Product" (
  "id", "restaurantId", "categoryId", "name", "description", "imageUrl", "price", "isActive", "createdAt", "updatedAt"
) VALUES
  (
    'seed-product-pizza', 'seed-restaurant', 'seed-category-pizzas', 'Pizza Burrata da Casa',
    'Massa de longa fermentacao, molho rustico, burrata fresca e pesto.',
    'https://images.unsplash.com/photo-1513104890138-7c749659a591?auto=format&fit=crop&w=1200&q=80',
    68.9, TRUE, NOW(), NOW()
  ),
  (
    'seed-product-burger', 'seed-restaurant', 'seed-category-burgers', 'Burger Smoke Cheddar',
    'Blend de costela, cheddar cremoso, cebola caramelizada e pao brioche.',
    'https://images.unsplash.com/photo-1568901346375-23c9450c58cd?auto=format&fit=crop&w=1200&q=80',
    42.9, TRUE, NOW(), NOW()
  ),
  (
    'seed-product-combo', 'seed-restaurant', 'seed-category-combos', 'Combo Almoco Executivo',
    'Prato principal, bebida e sobremesa de giro rapido.',
    'https://images.unsplash.com/photo-1544025162-d76694265947?auto=format&fit=crop&w=1200&q=80',
    36.5, TRUE, NOW(), NOW()
  );

INSERT INTO "ProductAdditional" (
  "id", "productId", "name", "price", "isActive"
) VALUES
  ('seed-additional-pizza', 'seed-product-pizza', 'Borda recheada', 8, TRUE),
  ('seed-additional-burger', 'seed-product-burger', 'Bacon extra', 5, TRUE),
  ('seed-additional-combo', 'seed-product-combo', 'Sobremesa do dia', 7.5, TRUE);

INSERT INTO "ProductVariation" (
  "id", "productId", "name", "price"
) VALUES
  ('seed-variation-pizza', 'seed-product-pizza', 'Grande', 68.9),
  ('seed-variation-burger', 'seed-product-burger', 'Duplo', 42.9),
  ('seed-variation-combo', 'seed-product-combo', 'Completo', 36.5);

INSERT INTO "Address" (
  "id", "userId", "label", "street", "number", "complement", "neighborhood", "city", "state", "zipCode", "latitude", "longitude", "isDefault", "createdAt", "updatedAt"
) VALUES (
  'seed-address-customer', 'seed-user-customer', 'Casa', 'Rua Vergueiro', '1543', 'Apto 82', 'Vila Mariana', 'Sao Paulo', 'SP', '04101-000',
  -23.589742, -46.634496, TRUE, NOW(), NOW()
);

INSERT INTO "Order" (
  "id", "customerId", "restaurantId", "addressId", "status", "subtotal", "deliveryFee", "serviceFee", "discountAmount", "total", "notes", "createdAt", "updatedAt"
) VALUES
  (
    'seed-order-delivered', 'seed-customer', 'seed-restaurant', 'seed-address-customer', 'DELIVERED',
    111.8, 8.9, 2.5, 0, 123.2, 'Pedido entregue com sucesso para validar historico real.', NOW() - INTERVAL '180 minutes', NOW()
  ),
  (
    'seed-order-ontheway', 'seed-customer', 'seed-restaurant', 'seed-address-customer', 'ON_THE_WAY',
    105.4, 7.5, 2.1, 0, 115, 'Pedido em rota para validar tracking ao vivo.', NOW() - INTERVAL '52 minutes', NOW()
  ),
  (
    'seed-order-pickup', 'seed-customer', 'seed-restaurant', 'seed-address-customer', 'DRIVER_ACCEPTED',
    68.9, 6.9, 1.8, 0, 77.6, 'Pedido aguardando retirada para validar fluxo da loja e do entregador.', NOW() - INTERVAL '18 minutes', NOW()
  );

INSERT INTO "OrderItem" (
  "id", "orderId", "productId", "quantity", "unitPrice", "totalPrice", "notes"
) VALUES
  ('seed-order-item-delivered-1', 'seed-order-delivered', 'seed-product-pizza', 1, 68.9, 68.9, 'Sem azeitona'),
  ('seed-order-item-delivered-2', 'seed-order-delivered', 'seed-product-burger', 1, 42.9, 42.9, 'Ponto da carne ao ponto'),
  ('seed-order-item-ontheway-1', 'seed-order-ontheway', 'seed-product-combo', 2, 36.5, 73, 'Com bebida zero'),
  ('seed-order-item-ontheway-2', 'seed-order-ontheway', 'seed-product-burger', 1, 42.9, 42.9, 'Sem cebola'),
  ('seed-order-item-pickup-1', 'seed-order-pickup', 'seed-product-pizza', 1, 68.9, 68.9, 'Cortar em 8 fatias');

INSERT INTO "Payment" (
  "id", "orderId", "method", "status", "externalReference", "amount", "createdAt", "updatedAt"
) VALUES
  ('seed-payment-delivered', 'seed-order-delivered', 'PIX', 'PAID', 'PIX-DELIVERED-001', 123.2, NOW() - INTERVAL '180 minutes', NOW()),
  ('seed-payment-ontheway', 'seed-order-ontheway', 'PIX', 'PAID', 'PIX-ONWAY-002', 115, NOW() - INTERVAL '52 minutes', NOW()),
  ('seed-payment-pickup', 'seed-order-pickup', 'PIX', 'PAID', 'PIX-PICKUP-003', 77.6, NOW() - INTERVAL '18 minutes', NOW());

INSERT INTO "Transaction" (
  "id", "paymentId", "type", "amount", "metadata", "createdAt"
) VALUES
  ('seed-transaction-delivered', 'seed-payment-delivered', 'PIX_CAPTURE', 123.2, '{"provider":"manual-seed"}'::jsonb, NOW() - INTERVAL '180 minutes'),
  ('seed-transaction-ontheway', 'seed-payment-ontheway', 'PIX_CAPTURE', 115, '{"provider":"manual-seed"}'::jsonb, NOW() - INTERVAL '52 minutes'),
  ('seed-transaction-pickup', 'seed-payment-pickup', 'PIX_CAPTURE', 77.6, '{"provider":"manual-seed"}'::jsonb, NOW() - INTERVAL '18 minutes');

INSERT INTO "Delivery" (
  "id", "orderId", "driverId", "status", "distanceKm", "etaMinutes", "baseFee", "pricePerKm", "deliveryFeeCustomer", "driverEarning", "platformDeliveryProfit",
  "demandMultiplier", "weatherMultiplier", "surgeMultiplier", "bonusAmount", "rainBonus", "peakBonus", "tipsAmount",
  "pickedUpAt", "deliveredAt", "createdAt", "updatedAt"
) VALUES
  (
    'seed-delivery-delivered', 'seed-order-delivered', 'seed-driver', 'DELIVERED', 6.4, 29, 5, 1.5, 8.9, 6.4, 2.5,
    1, 1, 1, 0, 0, 0, 0, NOW() - INTERVAL '145 minutes', NOW() - INTERVAL '128 minutes', NOW() - INTERVAL '180 minutes', NOW()
  ),
  (
    'seed-delivery-ontheway', 'seed-order-ontheway', 'seed-driver', 'ON_THE_WAY', 4.7, 14, 5, 1.5, 7.5, 5.5, 2,
    1, 1, 1, 0, 0, 0, 0, NOW() - INTERVAL '12 minutes', NULL, NOW() - INTERVAL '52 minutes', NOW()
  ),
  (
    'seed-delivery-pickup', 'seed-order-pickup', 'seed-driver', 'ACCEPTED', 3.2, 10, 5, 1.5, 6.9, 5, 1.9,
    1, 1, 1, 0, 0, 0, 0, NULL, NULL, NOW() - INTERVAL '18 minutes', NOW()
  );

INSERT INTO "DeliveryRoute" (
  "id", "orderId", "driverId", "restaurantLatitude", "restaurantLongitude", "customerLatitude", "customerLongitude",
  "pickupDistanceKm", "deliveryDistanceKm", "estimatedPickupTime", "estimatedDeliveryTime", "status", "createdAt", "updatedAt"
) VALUES
  (
    'seed-route-delivered', 'seed-order-delivered', 'seed-driver', -23.55052, -46.633308, -23.589742, -46.634496,
    1.2, 5.2, 6, 23, 'DELIVERED', NOW() - INTERVAL '180 minutes', NOW()
  ),
  (
    'seed-route-ontheway', 'seed-order-ontheway', 'seed-driver', -23.55052, -46.633308, -23.589742, -46.634496,
    0.8, 3.9, 4, 14, 'TO_CUSTOMER', NOW() - INTERVAL '52 minutes', NOW()
  ),
  (
    'seed-route-pickup', 'seed-order-pickup', 'seed-driver', -23.55052, -46.633308, -23.589742, -46.634496,
    1.1, 2.1, 5, 10, 'TO_RESTAURANT', NOW() - INTERVAL '18 minutes', NOW()
  );

INSERT INTO "DriverLocation" (
  "id", "driverId", "orderId", "latitude", "longitude", "heading", "speed", "accuracy", "createdAt"
) VALUES
  ('seed-driver-location-delivered', 'seed-driver', 'seed-order-delivered', -23.579742, -46.638, 120, 18, 6, NOW() - INTERVAL '132 minutes'),
  ('seed-driver-location-ontheway', 'seed-driver', 'seed-order-ontheway', -23.582451, -46.636211, 98, 24, 5, NOW() - INTERVAL '4 minutes'),
  ('seed-driver-location-pickup', 'seed-driver', 'seed-order-pickup', -23.55312, -46.63245, 45, 12, 4, NOW() - INTERVAL '2 minutes');

COMMIT;
