'use client';

import { useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import { Plus, Edit, DollarSign, Clock } from 'lucide-react';
import DataTable from '@/components/ui/DataTable';
import ActionButton from '@/components/ui/ActionButton';
import Badge from '@/components/ui/Badge';
import { agentsApi } from '@/lib/api';
import { formatCurrency, formatDateTime } from '@/lib/utils';

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

  const { data, isLoading } = useQuery({
    queryKey: ['agents', filters],
    queryFn: () => agentsApi.getAll(filters),
  });

  const columns = [
    {
      key: 'agentCode',
      label: 'Agent Code',
      sortable: true,
      render: (agent: any) => <span className="font-mono font-semibold">{agent.agentCode}</span>,
    },
    {
      key: 'user',
      label: 'Name',
      render: (agent: any) => (
        <div>
          <div className="font-medium">{agent.user?.fullName || 'N/A'}</div>
          <div className="text-sm text-gray-500">{agent.user?.email}</div>
        </div>
      ),
    },
    {
      key: 'commissionRate',
      label: 'Commission',
      render: (agent: any) => <span>{agent.commissionRate}%</span>,
    },
    {
      key: 'active',
      label: 'Status',
      render: (agent: any) => (
        <Badge variant="status" status={agent.active ? 'CONFIRMED' : 'CANCELLED'}>
          {agent.active ? 'Active' : 'Inactive'}
        </Badge>
      ),
    },
  ];

  const actions = [
    {
      label: 'View Shifts',
      onClick: (agent: any) => {
        window.location.href = `/agents/${agent.id}/shifts`;
      },
      variant: 'secondary' as const,
      icon: Clock,
    },
    {
      label: 'View Commissions',
      onClick: (agent: any) => {
        window.location.href = `/agents/${agent.id}/commissions`;
      },
      variant: 'secondary' as const,
      icon: DollarSign,
    },
    {
      label: 'Edit',
      onClick: (agent: any) => console.log('Edit', agent),
      variant: 'secondary' as const,
      icon: Edit,
    },
  ];

  return (
    <div className="space-y-6">
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-2xl font-bold">Agent Operations</h1>
          <p className="text-muted-foreground">Manage booking agents and their operations</p>
        </div>
        <ActionButton icon={Plus}>Add Agent</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 agents..."
              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.active}
              onChange={(e) => setFilters({ ...filters, active: e.target.value })}
            >
              <option value="">All Status</option>
              <option value="true">Active</option>
              <option value="false">Inactive</option>
            </select>
          </div>
        </div>
      </div>

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