import { Form, Head } from '@inertiajs/react';
import IntegrationController from '@/actions/App/Http/Controllers/Settings/IntegrationController';
import Heading from '@/components/heading';
import InputError from '@/components/input-error';
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import { Button } from '@/components/ui/button';
import { Label } from '@/components/ui/label';
import { edit as editIntegrations } from '@/routes/integrations';
import type { ExternalCompany } from '@/types/growth-score';

type Props = {
    company: {
        id: number;
        name: string;
        external_company_id: string | null;
        external_account_type: string | null;
    } | null;
    companies: ExternalCompany[];
    backendConfigured: boolean;
};

export default function Integrations({
    company,
    companies,
    backendConfigured,
}: Props) {
    const selectedId = company?.external_company_id
        ? `${company.external_account_type}:${company.external_company_id}`
        : '';

    return (
        <>
            <Head title="Integrations" />

            <h1 className="sr-only">Integrations</h1>

            <div className="space-y-6">
                <Heading
                    variant="small"
                    title="Accounting connection"
                    description="Link this Monily AI company to a QuickBooks or Xero client from Monily."
                />

                {!company && (
                    <Alert>
                        <AlertTitle>No company on this account</AlertTitle>
                        <AlertDescription>
                            A default company is required before you can save a
                            connection.
                        </AlertDescription>
                    </Alert>
                )}

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

                {company && (
                    <Form
                        {...IntegrationController.update.form()}
                        options={{ preserveScroll: true }}
                        className="space-y-6"
                    >
                        {({ processing, errors }) => (
                            <>
                                <div className="grid gap-2">
                                    <Label htmlFor="accounting-company">
                                        Linked company
                                    </Label>
                                    <select
                                        id="accounting-company"
                                        name="connection"
                                        defaultValue={selectedId}
                                        className="h-9 w-full rounded-md border border-input bg-background px-3 text-sm shadow-xs outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50"
                                    >
                                        <option value="">
                                            Select a company
                                        </option>
                                        {companies.map((item) => (
                                            <option
                                                key={`${item.account_type}:${item.id}`}
                                                value={`${item.account_type}:${item.id}`}
                                            >
                                                {item.name} (
                                                {item.account_type === 'xero'
                                                    ? 'Xero'
                                                    : 'QuickBooks'}
                                                )
                                            </option>
                                        ))}
                                    </select>
                                    <InputError
                                        message={
                                            errors.connection ||
                                            errors.external_company_id ||
                                            errors.external_account_type
                                        }
                                    />
                                </div>

                                <Button type="submit" disabled={processing}>
                                    Save connection
                                </Button>
                            </>
                        )}
                    </Form>
                )}
            </div>
        </>
    );
}

Integrations.layout = {
    breadcrumbs: [
        {
            title: 'Integrations',
            href: editIntegrations(),
        },
    ],
};
