import { Form, Head, Link } from '@inertiajs/react';
import Heading from '@/components/heading';
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { client, clients } from '@/routes/growth-score';
import type { ExternalCompany } from '@/types/growth-score';

type Props = {
    companies: ExternalCompany[];
    search: string;
    backendConfigured: boolean;
};

export default function ClientGrowthScore({
    companies,
    search,
    backendConfigured,
}: Props) {
    return (
        <>
            <Head title="Client Growth Score" />
            <div className="flex flex-col gap-6 p-4">
                <Heading
                    title="Analyze Client Growth Score"
                    description="Search the synced Monily client list, then open a company to see its historical growth score."
                />

                {!backendConfigured && (
                    <Alert>
                        <AlertTitle>Backend API is not configured</AlertTitle>
                        <AlertDescription>
                            Set MONILY_BACKEND_API_URL and
                            MONILY_BACKEND_API_TOKEN to load live clients.
                        </AlertDescription>
                    </Alert>
                )}

                <Form {...clients.form()} className="flex max-w-xl gap-2">
                    <Input
                        name="search"
                        defaultValue={search}
                        placeholder="Search clients"
                    />
                    <Button type="submit">Search</Button>
                </Form>

                <div className="overflow-hidden rounded-xl border">
                    <table className="w-full text-left text-sm">
                        <thead className="bg-muted/50 text-muted-foreground">
                            <tr>
                                <th className="px-4 py-3 font-medium">
                                    Company
                                </th>
                                <th className="px-4 py-3 font-medium">
                                    Source
                                </th>
                                <th className="px-4 py-3 font-medium" />
                            </tr>
                        </thead>
                        <tbody>
                            {companies.map((company) => (
                                <tr
                                    key={`${company.account_type}-${company.id}`}
                                    className="border-t"
                                >
                                    <td className="px-4 py-3">
                                        {company.name}
                                    </td>
                                    <td className="px-4 py-3">
                                        <Badge variant="secondary">
                                            {company.account_type === 'xero'
                                                ? 'Xero'
                                                : 'QuickBooks'}
                                        </Badge>
                                    </td>
                                    <td className="px-4 py-3 text-right">
                                        <Button
                                            asChild
                                            variant="outline"
                                            size="sm"
                                        >
                                            <Link
                                                href={client(company.id, {
                                                    query: {
                                                        account_type:
                                                            company.account_type,
                                                    },
                                                })}
                                            >
                                                Analyze
                                            </Link>
                                        </Button>
                                    </td>
                                </tr>
                            ))}
                            {companies.length === 0 && (
                                <tr>
                                    <td
                                        colSpan={3}
                                        className="px-4 py-8 text-center text-muted-foreground"
                                    >
                                        No clients matched that search.
                                    </td>
                                </tr>
                            )}
                        </tbody>
                    </table>
                </div>
            </div>
        </>
    );
}

ClientGrowthScore.layout = {
    breadcrumbs: [
        {
            title: 'Client Growth Score',
            href: clients(),
        },
    ],
};
