'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 { loyaltyApi } from '@/lib/api';
import { formatDateTime, formatCurrency } from '@/lib/utils';

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

  const { data, isLoading } = useQuery({
    queryKey: ['loyalty', filters],
    queryFn: () => loyaltyApi.getAccounts(filters),
  });

  const columns = [
      { key: 'passenger', label: 'Passenger', render: (account: any) => account.passenger?.fullName || 'N/A' },
      { key: 'tier', label: 'Tier', render: (account: any) => <Badge>{account.tier}</Badge> },
      { key: 'pointsBalance', label: 'Points', render: (account: any) => account.pointsBalance?.toLocaleString() || 0 },
      { key: 'lifetimePoints', label: 'Lifetime Points', render: (account: any) => account.lifetimePoints?.toLocaleString() || 0 },
    ];

  return (
    <div className="space-y-6">
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-2xl font-bold text-foreground">Loyalty Program</h1>
          <p className="text-muted-foreground">Manage loyalty accounts and rewards</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">Tier</label>
        <select className="input" value={filters.tier} onChange={(e) => setFilters({ ...filters, tier: e.target.value })}>
          <option value="">All Tiers</option>
          <option value="BRONZE">Bronze</option>
          <option value="SILVER">Silver</option>
          <option value="GOLD">Gold</option>
          <option value="PLATINUM">Platinum</option>
        </select>
      </div>
    
        </div>
      </div>

      <DataTable
        data={Array.isArray(data) ? data : (data?.items || [])}
        columns={columns}
        loading={isLoading}
        emptyMessage="No loyalty program found"
      />
    </div>
  );
}
