import type { Order, SellerOrderStats } from "./types";

const av = (id: string) =>
  `https://images.unsplash.com/photo-${id}?w=120&h=120&fit=crop&q=80`;
const img = (id: string) =>
  `https://images.unsplash.com/photo-${id}?w=400&h=300&fit=crop&q=80`;

export const MOCK_ORDERS: Order[] = [
  // Buyer: me, buying iPhone - currently SHIPPED
  {
    id: "ORD-2026-001",
    paymentOrderId: "ord-001",
    listingId: "bl1",
    listingSlug: "iphone-14-pro-max-256gb-deep-purple",
    listingTitle: "iPhone 14 Pro Max 256GB Deep Purple",
    listingImage: img("1510557880182-3d4d3cba35a5"),
    listingCondition: "Like New",
    sellerId: "s1",
    sellerName: "Rahul Sharma",
    sellerAvatar: av("1535713875002-d1d0cf377fde"),
    sellerVerified: true,
    sellerPhone: "+91 98765 00001",
    buyerId: "me",
    buyerName: "Keyur Ramani",
    buyerAvatar: av("1535713875002-d1d0cf377fde"),
    quantity: 1,
    listingPrice: 68000,
    totalAmount: 69904,
    paymentMethod: "paypal",
    paymentStatus: "paid",
    status: "shipped",
    timeline: [
      {
        status: "placed",
        timestamp: "2026-06-24T14:30:00Z",
        description: "Your order has been placed successfully. Payment of ₹69,904 confirmed via PayPal.",
        actor: "system",
      },
      {
        status: "confirmed",
        timestamp: "2026-06-24T16:00:00Z",
        description: "Seller confirmed your order and is preparing the package.",
        actor: "seller",
      },
      {
        status: "packed",
        timestamp: "2026-06-25T10:00:00Z",
        description: "Item has been securely packed. Original box and all accessories included.",
        actor: "seller",
      },
      {
        status: "shipped",
        timestamp: "2026-06-25T14:30:00Z",
        description: "Package handed over to Blue Dart Express. Tracking: BD123456789IN",
        actor: "seller",
      },
    ],
    shipping: {
      recipientName: "Keyur Ramani",
      address: "42, Sunrise Apartments, Andheri West",
      city: "Mumbai",
      pincode: "400053",
      phone: "+91 98765 43210",
      courierName: "Blue Dart Express",
      trackingNumber: "BD123456789IN",
      estimatedDelivery: "2026-06-27T18:00:00Z",
      shippingMethod: "express",
      currentLocation: "Mumbai Sorting Facility",
    },
    createdAt: "2026-06-24T14:30:00Z",
    updatedAt: "2026-06-25T14:30:00Z",
  },

  // Buyer: me, buying MacBook - DISPUTED
  {
    id: "ORD-2026-002",
    paymentOrderId: "ord-002",
    listingId: "bl2",
    listingSlug: "macbook-air-m2-13inch-midnight",
    listingTitle: "MacBook Air M2 13-inch — Midnight",
    listingImage: img("1517336714731-489689fd1ca8"),
    listingCondition: "Like New",
    sellerId: "s2",
    sellerName: "Priya Mehta",
    sellerAvatar: av("1494790108377-be9c29b29330"),
    sellerVerified: true,
    sellerPhone: "+91 98765 00002",
    buyerId: "me",
    buyerName: "Keyur Ramani",
    buyerAvatar: av("1535713875002-d1d0cf377fde"),
    quantity: 1,
    listingPrice: 70000,
    totalAmount: 71960,
    paymentMethod: "paypal",
    paymentStatus: "refunded",
    status: "disputed",
    timeline: [
      {
        status: "placed",
        timestamp: "2026-06-25T09:00:00Z",
        description: "Order placed. Payment of ₹71,960 confirmed via PayPal.",
        actor: "system",
      },
      {
        status: "confirmed",
        timestamp: "2026-06-25T10:00:00Z",
        description: "Seller confirmed the order.",
        actor: "seller",
      },
      {
        status: "packed",
        timestamp: "2026-06-25T12:00:00Z",
        description: "Item packed and ready for delivery.",
        actor: "seller",
      },
      {
        status: "delivered",
        timestamp: "2026-06-25T18:00:00Z",
        description: "Item delivered to buyer.",
        actor: "system",
      },
      {
        status: "disputed",
        timestamp: "2026-06-25T20:00:00Z",
        description: "Buyer raised a dispute: Item condition did not match description.",
        actor: "buyer",
      },
    ],
    shipping: {
      recipientName: "Keyur Ramani",
      address: "42, Sunrise Apartments, Andheri West",
      city: "Mumbai",
      pincode: "400053",
      phone: "+91 98765 43210",
      courierName: "Delhivery",
      trackingNumber: "DL887766554IN",
      estimatedDelivery: "2026-06-25T18:00:00Z",
      shippingMethod: "standard",
    },
    dispute: {
      reason: "Item condition did not match description",
      description: "The MacBook Air has visible scratches on the lid which were not shown in the listing photos. The condition was described as Like New but it clearly shows signs of heavy use.",
      status: "under_review",
      createdAt: "2026-06-25T20:00:00Z",
    },
    createdAt: "2026-06-25T09:00:00Z",
    updatedAt: "2026-06-26T10:00:00Z",
  },

  // Buyer: me, buying RE Motorcycle - CONFIRMED (just placed)
  {
    id: "ORD-2026-003",
    paymentOrderId: "ord-new-1",
    listingId: "bl4",
    listingSlug: "royal-enfield-classic-350-2022",
    listingTitle: "Royal Enfield Classic 350 — 2022",
    listingImage: img("1558618666-fcd25c85cd64"),
    listingCondition: "Good",
    sellerId: "s4",
    sellerName: "Vikram Singh",
    sellerAvatar: av("1507003211169-0a1dd7228f2d"),
    sellerVerified: true,
    sellerPhone: "+91 98765 00004",
    buyerId: "me",
    buyerName: "Keyur Ramani",
    buyerAvatar: av("1535713875002-d1d0cf377fde"),
    quantity: 1,
    listingPrice: 145000,
    totalAmount: 148280,
    paymentMethod: "cod",
    paymentStatus: "pending",
    status: "confirmed",
    timeline: [
      {
        status: "placed",
        timestamp: "2026-06-26T08:00:00Z",
        description: "COD order placed. Seller will confirm soon.",
        actor: "system",
      },
      {
        status: "confirmed",
        timestamp: "2026-06-26T09:30:00Z",
        description: "Seller confirmed your order. Arrange a meetup in Pune.",
        actor: "seller",
      },
    ],
    shipping: {
      recipientName: "Keyur Ramani",
      address: "42, Sunrise Apartments, Andheri West",
      city: "Mumbai",
      pincode: "400053",
      phone: "+91 98765 43210",
      shippingMethod: "self_pickup",
      estimatedDelivery: "2026-06-28T12:00:00Z",
    },
    createdAt: "2026-06-26T08:00:00Z",
    updatedAt: "2026-06-26T09:30:00Z",
  },

  // Seller: me, selling Canon EOS - PLACED (awaiting my confirmation)
  {
    id: "ORD-2026-004",
    paymentOrderId: "ord-003",
    listingId: "bl5",
    listingSlug: "canon-eos-r50-mirrorless-kit",
    listingTitle: "Canon EOS R50 + 18-45mm Kit Lens",
    listingImage: img("1502920917128-1aa500764cbd"),
    listingCondition: "Like New",
    sellerId: "me",
    sellerName: "Keyur Ramani",
    sellerAvatar: av("1535713875002-d1d0cf377fde"),
    sellerVerified: false,
    sellerPhone: "+91 98765 43210",
    buyerId: "b1",
    buyerName: "Arjun Nair",
    buyerAvatar: av("1500648767791-00dcc994a43e"),
    quantity: 1,
    listingPrice: 40000,
    totalAmount: 41120,
    paymentMethod: "cod",
    paymentStatus: "pending",
    status: "placed",
    timeline: [
      {
        status: "placed",
        timestamp: "2026-06-26T10:00:00Z",
        description: "Buyer placed a COD order for your Canon EOS R50.",
        actor: "system",
      },
    ],
    shipping: {
      recipientName: "Arjun Nair",
      address: "15, Palm Grove, Bandra",
      city: "Mumbai",
      pincode: "400050",
      phone: "+91 91234 56789",
      shippingMethod: "self_pickup",
    },
    createdAt: "2026-06-26T10:00:00Z",
    updatedAt: "2026-06-26T10:00:00Z",
  },

  // Seller: me, selling Sony Headphones - COMPLETED
  {
    id: "ORD-2026-005",
    paymentOrderId: "ord-004",
    listingId: "bl3",
    listingSlug: "sony-wh1000xm5-noise-cancelling",
    listingTitle: "Sony WH-1000XM5 Headphones",
    listingImage: img("1505740420928-5e560c06d30e"),
    listingCondition: "Good",
    sellerId: "me",
    sellerName: "Keyur Ramani",
    sellerAvatar: av("1535713875002-d1d0cf377fde"),
    sellerVerified: false,
    sellerPhone: "+91 98765 43210",
    buyerId: "b2",
    buyerName: "Meera Joshi",
    buyerAvatar: av("1438761681033-6461ffad8d80"),
    quantity: 1,
    listingPrice: 22000,
    totalAmount: 22616,
    paymentMethod: "cod",
    paymentStatus: "paid",
    status: "completed",
    timeline: [
      {
        status: "placed",
        timestamp: "2026-06-25T14:00:00Z",
        description: "Buyer placed a COD order.",
        actor: "system",
      },
      {
        status: "confirmed",
        timestamp: "2026-06-25T15:00:00Z",
        description: "You confirmed the order. Meetup arranged in Delhi.",
        actor: "seller",
      },
      {
        status: "delivered",
        timestamp: "2026-06-25T19:00:00Z",
        description: "Item delivered to buyer. Cash received.",
        actor: "seller",
      },
      {
        status: "completed",
        timestamp: "2026-06-25T20:00:00Z",
        description: "Buyer confirmed receipt. Order completed successfully.",
        actor: "buyer",
      },
    ],
    shipping: {
      recipientName: "Meera Joshi",
      address: "8, Rose Garden, Lajpat Nagar",
      city: "Delhi",
      pincode: "110024",
      phone: "+91 99887 76655",
      shippingMethod: "self_pickup",
    },
    createdAt: "2026-06-25T14:00:00Z",
    updatedAt: "2026-06-25T20:00:00Z",
  },
];

export const SELLER_STATS: SellerOrderStats = {
  total: 2,
  pending: 1,
  confirmed: 0,
  packed: 0,
  shipped: 0,
  delivered: 0,
  cancelled: 0,
  revenue: 22616,
};

// Status display config
export const STATUS_CONFIG: Record<
  string,
  { label: string; color: string; bg: string; description: string }
> = {
  placed: {
    label: "Order Placed",
    color: "#60a5fa",
    bg: "rgba(96,165,250,0.1)",
    description: "Your order has been received",
  },
  confirmed: {
    label: "Confirmed",
    color: "#fb923c",
    bg: "rgba(251,146,60,0.1)",
    description: "Seller has confirmed your order",
  },
  packed: {
    label: "Packed",
    color: "#a78bfa",
    bg: "rgba(167,139,250,0.1)",
    description: "Your item is packed and ready",
  },
  shipped: {
    label: "Shipped",
    color: "#f0c040",
    bg: "rgba(240,192,64,0.1)",
    description: "Your package is on the way",
  },
  out_for_delivery: {
    label: "Out for Delivery",
    color: "#f97316",
    bg: "rgba(249,115,22,0.1)",
    description: "Package arriving today",
  },
  delivered: {
    label: "Delivered",
    color: "#22c55e",
    bg: "rgba(34,197,94,0.1)",
    description: "Package delivered successfully",
  },
  completed: {
    label: "Completed",
    color: "#22c55e",
    bg: "rgba(34,197,94,0.1)",
    description: "Order completed successfully",
  },
  cancelled: {
    label: "Cancelled",
    color: "#6b7280",
    bg: "rgba(107,114,128,0.1)",
    description: "Order was cancelled",
  },
  disputed: {
    label: "Disputed",
    color: "#f87171",
    bg: "rgba(248,113,113,0.1)",
    description: "Dispute in progress",
  },
};

export const PIPELINE_STEPS: Array<{ status: string; label: string; icon: string }> = [
  { status: "placed", label: "Placed", icon: "📋" },
  { status: "confirmed", label: "Confirmed", icon: "✅" },
  { status: "packed", label: "Packed", icon: "📦" },
  { status: "shipped", label: "Shipped", icon: "🚚" },
  { status: "delivered", label: "Delivered", icon: "🏠" },
  { status: "completed", label: "Completed", icon: "⭐" },
];

export function formatOrderDate(iso: string): string {
  return new Date(iso).toLocaleDateString("en-IN", {
    day: "numeric",
    month: "short",
    year: "numeric",
  });
}

export function formatOrderTime(iso: string): string {
  return new Date(iso).toLocaleTimeString("en-IN", {
    hour: "2-digit",
    minute: "2-digit",
    hour12: true,
  });
}

export function formatOrderDateTime(iso: string): string {
  return `${formatOrderDate(iso)}, ${formatOrderTime(iso)}`;
}

export function formatPrice(n: number): string {
  return `₹${n.toLocaleString("en-IN")}`;
}

export function getPipelineStep(status: string): number {
  const steps = ["placed", "confirmed", "packed", "shipped", "out_for_delivery", "delivered", "completed"];
  return steps.indexOf(status);
}

export function isTerminalStatus(status: string): boolean {
  return ["completed", "cancelled", "disputed"].includes(status);
}
