'use client';

import { useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import { Download } from 'lucide-react';
import DataTable from '@/components/ui/DataTable';
import Badge from '@/components/ui/Badge';
import ActionButton from '@/components/ui/ActionButton';
import { foodApi } from '@/lib/api';
import { formatDateTime, formatCurrency } from '@/lib/utils';

export default function FoodPage() {
  const [filters, setFilters] = useState({ search: '', status: '' });

  const { data, isLoading } = useQuery({
    queryKey: ['food', filters],
    queryFn: () => foodApi.getOrders(filters),
  });

  const columns = [
      { key: 'orderNumber', label: 'Order #', render: (order: any) => <span className="font-mono">{order.orderNumber || order.id?.substring(0, 8)}</span> },
      { key: 'passenger', label: 'Passenger', render: (order: any) => order.passenger?.fullName || 'N/A' },
      { key: 'items', label: 'Items', render: (order: any) => order.items?.length || 0 },
      { key: 'totalMinor', label: 'Total', render: (order: any) => formatCurrency(order.totalMinor, 'ETB') },
      { key: 'status', label: 'Status', render: (order: any) => <Badge variant="status" status={order.status}>{order.status}</Badge> },
    ];

  return (
    <div className="space-y-6">
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-2xl font-bold text-foreground">Food & Dining</h1>
          <p className="text-muted-foreground">Manage food orders and menu items</p>
        </div>
        <ActionButton icon={Download} variant="secondary">Export</ActionButton>
      </div>

      <div className="card">
        <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
          
      <div>
        <label className="label">Search</label>
        <input type="text" placeholder="Search..." className="input" value={filters.search} onChange={(e) => setFilters({ ...filters, search: e.target.value })} />
      </div>
      <div>
        <label className="label">Status</label>
        <select className="input" value={filters.status} onChange={(e) => setFilters({ ...filters, status: e.target.value })}>
          <option value="">All Status</option>
          <option value="PENDING">Pending</option>
          <option value="PREPARING">Preparing</option>
          <option value="READY">Ready</option>
          <option value="DELIVERED">Delivered</option>
        </select>
      </div>
    
        </div>
      </div>

      <DataTable
        data={data?.items || data || []}
        columns={columns}
        loading={isLoading}
        emptyMessage="No food & dining found"
      />
    </div>
  );
}
