import { useState, useEffect } from 'react';
import './styles.css';

interface OrderItem {
  id: string;
  name: string;
  quantity: number;
  price: number;
  modifiers: { name: string; price: number }[];
  instructions?: string;
}

interface Order {
  id: string;
  orderNumber: string;
  status: string;
  type: string;
  customer: string;
  server: string;
  table?: string;
  items: OrderItem[];
  subtotal: number;
  tax: number;
  tip: number;
  total: number;
  paymentMethod: string;
  createdAt: string;
  completedAt?: string;
}

export default function OrderDetail() {
  const [order, setOrder] = useState<Order | null>(null);

  useEffect(() => {
    // Mock data
    setOrder({
      id: '1',
      orderNumber: 'ORD-142',
      status: 'in_progress',
      type: 'dine_in',
      customer: 'Sarah Johnson',
      server: 'Mike Chen',
      table: 'Table 12',
      items: [
        {
          id: '1',
          name: 'Classic Burger',
          quantity: 2,
          price: 14.99,
          modifiers: [
            { name: 'Extra Cheese', price: 1.50 },
            { name: 'No Onions', price: 0 },
          ],
          instructions: 'Medium rare',
        },
        {
          id: '2',
          name: 'Caesar Salad',
          quantity: 1,
          price: 9.99,
          modifiers: [
            { name: 'Add Chicken', price: 4.00 },
          ],
        },
        {
          id: '3',
          name: 'French Fries',
          quantity: 2,
          price: 4.99,
          modifiers: [],
        },
        {
          id: '4',
          name: 'Soft Drink',
          quantity: 2,
          price: 2.99,
          modifiers: [],
        },
      ],
      subtotal: 60.43,
      tax: 5.44,
      tip: 12.00,
      total: 77.87,
      paymentMethod: 'Credit Card',
      createdAt: new Date(Date.now() - 15 * 60000).toISOString(),
    });
  }, []);

  if (!order) {
    return <div className="loading">Loading order...</div>;
  }

  const getStatusColor = (status: string) => {
    const colors: Record<string, string> = {
      draft: '#6b7280',
      pending: '#fbbf24',
      in_progress: '#3b82f6',
      ready: '#10b981',
      completed: '#10b981',
      voided: '#ef4444',
    };
    return colors[status] || '#6b7280';
  };

  return (
    <div className="app">
      <header className="app-header">
        <div>
          <h1>🧾 Order Detail</h1>
          <p>{order.orderNumber}</p>
        </div>
        <span
          className="status-badge"
          style={{ backgroundColor: getStatusColor(order.status) }}
        >
          {order.status.replace('_', ' ').toUpperCase()}
        </span>
      </header>

      <div className="order-grid">
        <div className="info-section">
          <h2>Order Information</h2>
          <div className="info-grid">
            <div className="info-item">
              <span className="info-label">Order Type</span>
              <span className="info-value">{order.type.replace('_', ' ')}</span>
            </div>
            <div className="info-item">
              <span className="info-label">Customer</span>
              <span className="info-value">{order.customer}</span>
            </div>
            <div className="info-item">
              <span className="info-label">Server</span>
              <span className="info-value">{order.server}</span>
            </div>
            <div className="info-item">
              <span className="info-label">Table</span>
              <span className="info-value">{order.table || 'N/A'}</span>
            </div>
            <div className="info-item">
              <span className="info-label">Created</span>
              <span className="info-value">
                {new Date(order.createdAt).toLocaleString()}
              </span>
            </div>
            <div className="info-item">
              <span className="info-label">Payment</span>
              <span className="info-value">{order.paymentMethod}</span>
            </div>
          </div>
        </div>

        <div className="items-section">
          <h2>Order Items</h2>
          <div className="items-list">
            {order.items.map((item) => (
              <div key={item.id} className="item-card">
                <div className="item-header">
                  <span className="item-name">
                    {item.quantity}× {item.name}
                  </span>
                  <span className="item-price">
                    ${(item.price * item.quantity).toFixed(2)}
                  </span>
                </div>
                {item.modifiers.length > 0 && (
                  <div className="modifiers">
                    {item.modifiers.map((mod, idx) => (
                      <div key={idx} className="modifier">
                        • {mod.name}
                        {mod.price > 0 && ` (+$${mod.price.toFixed(2)})`}
                      </div>
                    ))}
                  </div>
                )}
                {item.instructions && (
                  <div className="instructions">📝 {item.instructions}</div>
                )}
              </div>
            ))}
          </div>

          <div className="totals">
            <div className="total-row">
              <span>Subtotal</span>
              <span>${order.subtotal.toFixed(2)}</span>
            </div>
            <div className="total-row">
              <span>Tax</span>
              <span>${order.tax.toFixed(2)}</span>
            </div>
            <div className="total-row">
              <span>Tip</span>
              <span>${order.tip.toFixed(2)}</span>
            </div>
            <div className="total-row total">
              <span>Total</span>
              <span>${order.total.toFixed(2)}</span>
            </div>
          </div>

          <div className="actions">
            <button className="btn btn-secondary">Print Receipt</button>
            <button className="btn btn-secondary">Refund</button>
            <button className="btn btn-primary">Update Status</button>
          </div>
        </div>
      </div>
    </div>
  );
}
