import ClientLayout from '@/Layouts/ClientLayout';
import { PageProps } from '@/types';
import { Head, useForm } from '@inertiajs/react';
import { Building2, Globe, Users } from 'lucide-react';
import SetupStepper from './components/SetupStepper';

const ORG_TYPES = [
    {
        value: 'sacco',
        label: 'SACCO',
        description: 'Savings & Credit Co-operative',
    },
    {
        value: 'microfinance',
        label: 'Microfinance',
        description: 'Microfinance Institution (MFI)',
    },
    {
        value: 'bank',
        label: 'Bank',
        description: 'Commercial or Community Bank',
    },
    {
        value: 'insurance',
        label: 'Insurance',
        description: 'Insurance Company or MGA',
    },
    {
        value: 'other',
        label: 'Other',
        description: 'Other financial institution',
    },
] as const;

const COUNTRIES = [
    { code: 'KE', name: 'Kenya' },
    { code: 'UG', name: 'Uganda' },
    { code: 'TZ', name: 'Tanzania' },
    { code: 'RW', name: 'Rwanda' },
    { code: 'ET', name: 'Ethiopia' },
    { code: 'GH', name: 'Ghana' },
    { code: 'NG', name: 'Nigeria' },
    { code: 'ZA', name: 'South Africa' },
    { code: 'ZM', name: 'Zambia' },
    { code: 'ZW', name: 'Zimbabwe' },
    { code: 'MW', name: 'Malawi' },
    { code: 'MZ', name: 'Mozambique' },
    { code: 'Other', name: 'Other' },
];

type Props = PageProps<{
    tenant: { name: string; country: string; org_type: string | null } | null;
    tenantId: string | null;
}>;

export default function Organisation({ tenant, tenantId }: Props) {
    const isEdit = tenantId !== null;

    const { data, setData, post, patch, processing, errors } = useForm({
        name: tenant?.name ?? '',
        country: tenant?.country ?? 'KE',
        org_type: tenant?.org_type ?? '',
    });

    function submit(e: React.FormEvent) {
        e.preventDefault();
        if (isEdit) {
            patch(route('setup.update', { tenant: tenantId }));
        } else {
            post(route('setup.create.store'));
        }
    }

    return (
        <ClientLayout
            title={isEdit ? 'Edit Organisation' : 'Setup — Organisation'}
        >
            <Head
                title={isEdit ? 'Edit Organisation' : 'Setup: Organisation'}
            />

            <div className="mx-auto max-w-2xl space-y-6 p-6 lg:p-8">
                {!isEdit && <SetupStepper current={1} />}

                <div className="pf-glass p-6 sm:p-8">
                    <div className="mb-6 flex items-center gap-3">
                        <span className="bg-primary/10 flex h-10 w-10 items-center justify-center rounded-xl">
                            <Building2 className="text-primary h-5 w-5" />
                        </span>
                        <div>
                            <h1 className="text-foreground text-lg font-semibold">
                                {isEdit
                                    ? 'Edit organisation details'
                                    : 'Tell us about your organisation'}
                            </h1>
                            <p className="text-muted-foreground text-sm">
                                This is the name your clients will see.
                            </p>
                        </div>
                    </div>

                    <form onSubmit={submit} className="space-y-5">
                        {/* Org name */}
                        <div className="space-y-1.5">
                            <label className="text-foreground text-sm font-medium">
                                Organisation name
                                <span className="text-destructive ml-0.5">
                                    *
                                </span>
                            </label>
                            <input
                                type="text"
                                value={data.name}
                                onChange={(e) =>
                                    setData('name', e.target.value)
                                }
                                placeholder="e.g. Kilimo SACCO"
                                className="border-border bg-background text-foreground placeholder:text-muted-foreground dark:placeholder:text-muted-foreground/50 focus:ring-primary/30 w-full rounded-xl border px-4 py-2.5 text-sm outline-none focus:ring-2"
                            />
                            {errors.name && (
                                <p className="text-destructive text-xs">
                                    {errors.name}
                                </p>
                            )}
                        </div>

                        {/* Country */}
                        <div className="space-y-1.5">
                            <label className="text-foreground flex items-center gap-1.5 text-sm font-medium">
                                <Globe className="text-muted-foreground h-3.5 w-3.5" />
                                Country
                                <span className="text-destructive ml-0.5">
                                    *
                                </span>
                            </label>
                            <select
                                value={data.country}
                                onChange={(e) =>
                                    setData('country', e.target.value)
                                }
                                className="border-border bg-background text-foreground focus:ring-primary/30 w-full rounded-xl border px-4 py-2.5 text-sm outline-none focus:ring-2"
                            >
                                {COUNTRIES.map(({ code, name }) => (
                                    <option key={code} value={code}>
                                        {name}
                                    </option>
                                ))}
                            </select>
                            {errors.country && (
                                <p className="text-destructive text-xs">
                                    {errors.country}
                                </p>
                            )}
                        </div>

                        {/* Org type */}
                        <div className="space-y-2">
                            <label className="text-foreground flex items-center gap-1.5 text-sm font-medium">
                                <Users className="text-muted-foreground h-3.5 w-3.5" />
                                Organisation type
                                <span className="text-destructive ml-0.5">
                                    *
                                </span>
                            </label>
                            <div className="grid grid-cols-1 gap-2 sm:grid-cols-2 lg:grid-cols-3">
                                {ORG_TYPES.map(
                                    ({ value, label, description }) => (
                                        <button
                                            key={value}
                                            type="button"
                                            onClick={() =>
                                                setData('org_type', value)
                                            }
                                            className={`rounded-xl border px-4 py-3 text-left transition-all ${
                                                data.org_type === value
                                                    ? 'border-primary bg-primary/5 ring-primary/30 ring-2'
                                                    : 'border-border hover:border-primary/40 hover:bg-muted/30'
                                            }`}
                                        >
                                            <p className="text-foreground text-sm font-medium">
                                                {label}
                                            </p>
                                            <p className="text-muted-foreground mt-0.5 text-xs">
                                                {description}
                                            </p>
                                        </button>
                                    ),
                                )}
                            </div>
                            {errors.org_type && (
                                <p className="text-destructive text-xs">
                                    {errors.org_type}
                                </p>
                            )}
                        </div>

                        <div className="flex items-center justify-between pt-2">
                            {isEdit ? (
                                <a
                                    href={route('dashboard')}
                                    className="text-muted-foreground hover:text-foreground text-sm transition-colors"
                                >
                                    ← Cancel
                                </a>
                            ) : (
                                <span />
                            )}
                            <button
                                type="submit"
                                disabled={
                                    processing || !data.name || !data.org_type
                                }
                                className="pf-btn px-6 py-2.5 text-sm disabled:cursor-not-allowed disabled:opacity-50"
                            >
                                {processing
                                    ? 'Saving…'
                                    : isEdit
                                      ? 'Save changes'
                                      : 'Continue →'}
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </ClientLayout>
    );
}
