import { useEffect, useState } from 'react';
import Head from 'next/head';
import Link from 'next/link';

/**
 * Simple Agent Offers Dashboard
 * Lists all offers for properties owned by the agent.
 */
export default function AgentOffers() {
  const [offers, setOffers] = useState<any[]>([]);
  const agentId = 1; // TODO: replace with actual logged‑in agent ID

  useEffect(() => {
    fetch(`/api/offers/agent/${agentId}`)
      .then((res) => res.json())
      .then(setOffers)
      .catch(console.error);
  }, []);

  const handleAction = async (id: number, action: 'accept' | 'reject') => {
    const res = await fetch(`/api/offers/${id}/${action}`, {
      method: 'POST',
    });
    if (res.ok) {
      setOffers((prev) => prev.map((o) => (o.id === id ? { ...o, status: action === 'accept' ? 'accepted' : 'rejected' } : o)));
    } else {
      alert('Action failed');
    }
  };

  return (
    <>
      <Head>
        <title>Agent Offers Dashboard</title>
      </Head>
      <div className="p-4 min-h-screen bg-background text-foreground">
        <h1 className="text-2xl mb-4">Offers for My Properties</h1>
        {offers.length === 0 ? (
          <p>No offers yet.</p>
        ) : (
          <table className="w-full table-auto border">
            <thead>
              <tr className="bg-primary text-white">
                <th className="p-2 border">ID</th>
                <th className="p-2 border">Property</th>
                <th className="p-2 border">Client</th>
                <th className="p-2 border">Price</th>
                <th className="p-2 border">Status</th>
                <th className="p-2 border">Actions</th>
              </tr>
            </thead>
            <tbody>
              {offers.map((o) => (
                <tr key={o.id} className="border-t">
                  <td className="p-2 border">{o.id}</td>
                  <td className="p-2 border">{o.property?.title ?? 'N/A'}</td>
                  <td className="p-2 border">{o.client?.name ?? 'N/A'}</td>
                  <td className="p-2 border">{o.offeredPrice}</td>
                  <td className="p-2 border capitalize">{o.status}</td>
                  <td className="p-2 border">
                    {o.status === 'pending' && (
                      <>
                        <button
                          onClick={() => handleAction(o.id, 'accept')}
                          className="mr-2 btn bg-primary text-white px-2 py-1"
                        >
                          Accept
                        </button>
                        <button
                          onClick={() => handleAction(o.id, 'reject')}
                          className="btn bg-gray-400 text-black px-2 py-1"
                        >
                          Reject
                        </button>
                      </>
                    )}
                  </td>
                </tr>
              ))}
            </tbody>
          </table>
        )}
        <Link href="/agent/create">
          <a className="mt-4 inline-block btn bg-secondary text-white px-4 py-2">Create New Property</a>
        </Link>
      </div>
    </>
  );
}
