'use client'

import { useEffect, useState } from 'react'
import dayjs from 'dayjs'
import Link from 'next/link'
import { formatCurrency, type ApiPaginated } from '@rentaldrivego/types'
import { apiFetch } from '@/lib/api'

interface ReservationRow {
  id: string
  status: string
  source: string
  startDate: string
  endDate: string
  totalAmount: number
  totalDays: number
  vehicle: { make: string; model: string }
  customer: { firstName: string; lastName: string; email: string }
}

export default function ReservationsPage() {
  const [rows, setRows] = useState<ReservationRow[]>([])
  const [error, setError] = useState<string | null>(null)

  useEffect(() => {
    apiFetch<ApiPaginated<ReservationRow>>('/reservations?pageSize=100')
      .then((result) => setRows(result.data ?? []))
      .catch((err) => setError(err.message))
  }, [])

  return (
    <div className="space-y-6">
      <div>
        <h2 className="text-xl font-semibold text-slate-900">Reservations</h2>
        <p className="text-sm text-slate-500 mt-1">All booking sources, including dashboard, public site, and marketplace.</p>
      </div>
      <div className="card overflow-hidden">
        {error ? (
          <div className="p-8 text-sm text-red-600">{error}</div>
        ) : (
          <div className="overflow-x-auto">
            <table className="w-full">
              <thead>
                <tr className="bg-slate-50 border-b border-slate-200">
                  <th className="text-left px-6 py-3 text-xs font-medium text-slate-500 uppercase tracking-wide">Customer</th>
                  <th className="text-left px-6 py-3 text-xs font-medium text-slate-500 uppercase tracking-wide">Vehicle</th>
                  <th className="text-left px-6 py-3 text-xs font-medium text-slate-500 uppercase tracking-wide">Dates</th>
                  <th className="text-left px-6 py-3 text-xs font-medium text-slate-500 uppercase tracking-wide">Source</th>
                  <th className="text-left px-6 py-3 text-xs font-medium text-slate-500 uppercase tracking-wide">Status</th>
                  <th className="text-right px-6 py-3 text-xs font-medium text-slate-500 uppercase tracking-wide">Total</th>
                </tr>
              </thead>
              <tbody className="divide-y divide-slate-100">
                {rows.map((row) => (
                  <tr key={row.id}>
                    <td className="px-6 py-4">
                      <Link href={`/dashboard/reservations/${row.id}`} className="text-sm font-semibold text-slate-900 hover:text-blue-700">
                        {row.customer.firstName} {row.customer.lastName}
                      </Link>
                      <p className="text-xs text-slate-500">{row.customer.email}</p>
                    </td>
                    <td className="px-6 py-4 text-sm text-slate-700">{row.vehicle.make} {row.vehicle.model}</td>
                    <td className="px-6 py-4 text-sm text-slate-500">{dayjs(row.startDate).format('MMM D')} - {dayjs(row.endDate).format('MMM D, YYYY')}</td>
                    <td className="px-6 py-4 text-sm text-slate-700">{row.source}</td>
                    <td className="px-6 py-4"><span className="badge-blue">{row.status}</span></td>
                    <td className="px-6 py-4 text-right text-sm font-semibold text-slate-900">{formatCurrency(row.totalAmount, 'MAD')}</td>
                  </tr>
                ))}
                {rows.length === 0 && (
                  <tr>
                    <td colSpan={6} className="px-6 py-10 text-center text-sm text-slate-400">No reservations yet.</td>
                  </tr>
                )}
              </tbody>
            </table>
          </div>
        )}
      </div>
    </div>
  )
}
