import { ArrowLeft } from 'lucide-react';

import { Head, Link } from '@inertiajs/react';

import SiteNav from '@/Components/Navigation/SiteNav';
import { Button } from '@/Components/ui/button';
import DashboardLayout from '@/Layouts/DashboardLayout';
import { formatNumber, formatPercent } from '@/lib/format';

interface PageData {
  page: string;
  clicks: number;
  impressions: number;
  ctr: number;
  position: number;
}

interface QueryData {
  query: string;
  clicks: number;
  impressions: number;
  ctr: number;
  position: number;
}

interface Props {
  site: {
    id: number;
    name: string;
    domain: string;
  };
  country: string;
  pages: PageData[];
  queries: QueryData[];
}

export default function GeographicDetail({ site, country, pages, queries }: Props) {
  return (
    <>
      <Head title={`${site.name} - ${country} Performance`} />

      <div className="container py-6">
        <SiteNav
          siteId={site.id}
          canAnalyze
          canRecommend
          canCannibalization
          canOpportunityMap
          canGeographic
          canDevice
        />

        <div className="mb-4">
          <Link href={route('geographic.index', { site: site.id })}>
            <Button variant="ghost" size="sm">
              <ArrowLeft className="mr-2 h-4 w-4" />
              Back to Geographic Dashboard
            </Button>
          </Link>
        </div>

        <h1 className="text-2xl font-bold tracking-tight">{country} Performance</h1>
        <p className="mt-1 text-sm text-muted-foreground">
          Detailed breakdown of pages and queries driving traffic from {country}
        </p>

        <div className="mt-6 space-y-6">
          {/* Top Pages Section */}
          <section className="rounded-lg border bg-card p-6">
            <h2 className="mb-4 text-lg font-semibold">Top Pages</h2>
            {pages.length === 0 ? (
              <p className="text-sm text-muted-foreground">No page data available</p>
            ) : (
              <div className="overflow-x-auto rounded-lg border">
                <table className="w-full min-w-[600px] text-sm">
                  <thead>
                    <tr className="border-b bg-muted/50">
                      <th className="text-left p-2 font-medium">Page</th>
                      <th className="text-right p-2 font-medium">Clicks</th>
                      <th className="text-right p-2 font-medium">Impressions</th>
                      <th className="text-right p-2 font-medium">CTR</th>
                      <th className="text-right p-2 font-medium">Position</th>
                    </tr>
                  </thead>
                  <tbody>
                    {pages.map((page, index) => (
                      <tr key={index} className="border-b last:border-0">
                        <td className="p-2 max-w-md truncate" title={page.page}>
                          {page.page}
                        </td>
                        <td className="text-right p-2">{formatNumber(page.clicks)}</td>
                        <td className="text-right p-2">{formatNumber(page.impressions)}</td>
                        <td className="text-right p-2">{formatPercent(page.ctr, 2)}</td>
                        <td className="text-right p-2">{page.position.toFixed(2)}</td>
                      </tr>
                    ))}
                  </tbody>
                </table>
              </div>
            )}
          </section>

          {/* Top Queries Section */}
          <section className="rounded-lg border bg-card p-6">
            <h2 className="mb-4 text-lg font-semibold">Top Queries</h2>
            {queries.length === 0 ? (
              <p className="text-sm text-muted-foreground">No query data available</p>
            ) : (
              <div className="overflow-x-auto rounded-lg border">
                <table className="w-full min-w-[600px] text-sm">
                  <thead>
                    <tr className="border-b bg-muted/50">
                      <th className="text-left p-2 font-medium">Query</th>
                      <th className="text-right p-2 font-medium">Clicks</th>
                      <th className="text-right p-2 font-medium">Impressions</th>
                      <th className="text-right p-2 font-medium">CTR</th>
                      <th className="text-right p-2 font-medium">Position</th>
                    </tr>
                  </thead>
                  <tbody>
                    {queries.map((query, index) => (
                      <tr key={index} className="border-b last:border-0">
                        <td className="p-2">{query.query}</td>
                        <td className="text-right p-2">{formatNumber(query.clicks)}</td>
                        <td className="text-right p-2">{formatNumber(query.impressions)}</td>
                        <td className="text-right p-2">{formatPercent(query.ctr, 2)}</td>
                        <td className="text-right p-2">{query.position.toFixed(2)}</td>
                      </tr>
                    ))}
                  </tbody>
                </table>
              </div>
            )}
          </section>
        </div>
      </div>
    </>
  );
}

GeographicDetail.layout = (page: React.ReactNode) => <DashboardLayout>{page}</DashboardLayout>;
