import { Head, Link, useForm } from '@inertiajs/react';
import { ArrowLeft, Calculator } from 'lucide-react';
import { useMemo, useState } from 'react';

import InputError from '@/components/input-error';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Checkbox } from '@/components/ui/checkbox';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import {
    Select,
    SelectContent,
    SelectGroup,
    SelectItem,
    SelectLabel,
    SelectTrigger,
    SelectValue,
} from '@/components/ui/select';
import { StatusBadge } from '@/components/ui/status-badge';
import { usePermissions } from '@/hooks/use-permissions';
import AppLayout from '@/layouts/app-layout';
import { formatDateID, formatIDR } from '@/lib/formatters';
import { toastError, toastLoading, toastSuccess } from '@/lib/toast';
import financingsRoutes from '@/routes/financing';
import type { BreadcrumbItem } from '@/types';
import type {
    Financing,
    FinancingInstallment,
    PaymentMethod,
} from '@/types/financing';

interface PageProps {
    financing: Financing & {
        installments: FinancingInstallment[];
    };
    auth: {
        user: {
            role: string;
        };
    };
}

const breadcrumbs: BreadcrumbItem[] = [
    {
        title: 'Pembiayaan',
        href: financingsRoutes.index().url,
    },
    {
        title: 'Catat Pembayaran',
        href: '',
    },
];

export default function RecordPayment({ financing }: PageProps) {
    const { can, isAdmin } = usePermissions();
    const isTeller = can('financing.record_payment') || isAdmin();

    console.log(financing);

    const form = useForm({
        financing_id: financing.id.toString(),
        branch_id: financing.branch_id?.toString() || '',
        amount: financing.installment_amount || '',
        payment_method: 'cash' as PaymentMethod,
        payment_date: new Date().toISOString().split('T')[0],
        installment_ids: [] as number[],
        notes: 'Pembayaran Angsuran',
    });

    const [allocationMode, setAllocationMode] = useState<'auto' | 'manual'>(
        'auto',
    );

    // Get pending/partial installments sorted by due date
    const pendingInstallments = useMemo(() => {
        return financing.installments
            .filter((i) => i.status === 'pending' || i.status === 'partial')
            .sort(
                (a, b) =>
                    new Date(a.due_date).getTime() -
                    new Date(b.due_date).getTime(),
            );
    }, [financing.installments]);

    // Auto-allocation calculation (FIFO - oldest first)
    const autoAllocation = useMemo(() => {
        if (allocationMode === 'manual' || !form.data.amount) return null;

        const amount = Number(form.data.amount);
        if (amount <= 0) return null;

        let remaining = amount;
        const allocation = pendingInstallments
            .map((installment) => {
                if (remaining <= 0) {
                    return {
                        installment,
                        principal_amount: 0,
                        margin_amount: 0,
                        total: 0,
                    };
                }

                const outstanding =
                    installment.total_amount - installment.paid_total;
                const allocated = Math.min(remaining, outstanding);

                // Allocate proportionally to principal and margin
                const principalRatio =
                    installment.principal_amount / installment.total_amount;
                const marginRatio =
                    installment.margin_amount / installment.total_amount;

                const principalPortion = allocated * principalRatio;
                const marginPortion = allocated * marginRatio;

                remaining -= allocated;

                return {
                    installment,
                    principal_amount: principalPortion,
                    margin_amount: marginPortion,
                    total: allocated,
                };
            })
            .filter((a) => a.total > 0);

        return {
            allocation,
            remaining: Math.max(0, remaining),
        };
    }, [form.data.amount, pendingInstallments, allocationMode]);

    // Manual allocation total
    const manualTotal = useMemo(() => {
        return form.data.installment_ids.reduce((total, id) => {
            const installment = pendingInstallments.find((i) => i.id === id);
            if (!installment) return total;

            const outstanding =
                installment.total_amount - installment.paid_total;
            return total + outstanding;
        }, 0);
    }, [form.data.installment_ids, pendingInstallments]);

    // Next due date
    const nextDue = pendingInstallments.find((i) => i.status === 'pending');
    const totalOverdue = pendingInstallments.filter(
        (i) => new Date(i.due_date) < new Date() && i.status !== 'paid',
    ).length;

    const handleSubmit = (e: { preventDefault: () => void }) => {
        e.preventDefault();

        const amount = Number(form.data.amount);

        // Validation
        if (amount <= 0) {
            toastError('Jumlah pembayaran harus lebih dari 0');
            return;
        }

        if (allocationMode === 'manual') {
            if (form.data.installment_ids.length === 0) {
                toastError('Pilih minimal satu angsuran untuk mode manual');
                return;
            }
            if (Math.abs(manualTotal - amount) > 100) {
                toastError(
                    `Jumlah pembayaran (${formatIDR(amount)}) tidak sesuai dengan total angsuran terpilih (${formatIDR(manualTotal)})`,
                );
                return;
            }
        }

        form.post(financingsRoutes.payments.record().url, {
            onStart: () => toastLoading('Mencatat pembayaran...'),
            onSuccess: () => toastSuccess('Pembayaran berhasil dicatat'),
            onError: () => toastError('Gagal mencatat pembayaran'),
        });
    };

    const handleQuickAmount = (amount: number) => {
        form.setData('amount', amount.toString());
    };

    const toggleInstallment = (id: number) => {
        const current = form.data.installment_ids;
        let newIds: number[];

        if (current.includes(id)) {
            newIds = current.filter((i) => i !== id);
        } else {
            newIds = [...current, id];
        }

        // Calculate new total based on selected installments
        const newTotal = newIds.reduce((total, installmentId) => {
            const installment = pendingInstallments.find(
                (i) => i.id === installmentId,
            );
            if (!installment) return total;

            const outstanding =
                installment.total_amount - installment.paid_total;
            return total + outstanding;
        }, 0);

        // Update both installment_ids and amount in one call
        form.setData({
            ...form.data,
            installment_ids: newIds,
            amount: newTotal.toString(),
        });
    };

    if (!isTeller) {
        return (
            <AppLayout breadcrumbs={breadcrumbs}>
                <Head title="Akses Ditolak" />
                <div className="flex h-full items-center justify-center">
                    <Card className="max-w-md">
                        <CardHeader>
                            <CardTitle className="text-destructive">
                                Akses Ditolak
                            </CardTitle>
                        </CardHeader>
                        <CardContent>
                            <p>
                                Anda tidak memiliki izin untuk mencatat
                                pembayaran.
                            </p>
                        </CardContent>
                    </Card>
                </div>
            </AppLayout>
        );
    }

    return (
        <AppLayout breadcrumbs={breadcrumbs}>
            <Head title={`Catat Pembayaran - ${financing.account_number}`} />

            <div className="flex h-full flex-1 flex-col gap-4 rounded-xl p-4">
                {/* Header */}
                <div className="flex items-center gap-4">
                    <Button variant="outline" size="icon" asChild>
                        <Link
                            href={
                                financingsRoutes.show({
                                    financing: financing.id,
                                }).url
                            }
                        >
                            <ArrowLeft className="h-4 w-4" />
                        </Link>
                    </Button>
                    <div className="flex flex-col gap-1">
                        <h1 className="text-2xl font-black">
                            Catat Pembayaran
                        </h1>
                        <span className="text-sm text-muted-foreground">
                            {financing.account_number} • {financing.member.name}
                        </span>
                    </div>
                </div>

                <div className="grid gap-6 lg:grid-cols-3">
                    {/* Form Column */}
                    <div className="space-y-6 lg:col-span-2">
                        <form onSubmit={handleSubmit} className="space-y-6">
                            {/* Payment Form */}
                            <Card>
                                <CardHeader>
                                    <CardTitle>Detail Pembayaran</CardTitle>
                                </CardHeader>
                                <CardContent className="space-y-4">
                                    {/* Amount */}
                                    <div className="space-y-2">
                                        <Label htmlFor="amount">
                                            Jumlah Pembayaran{' '}
                                            <span className="text-destructive">
                                                *
                                            </span>
                                        </Label>
                                        <Input
                                            id="amount"
                                            type="number"
                                            min={0}
                                            step={1000}
                                            value={form.data.amount}
                                            onChange={(e) =>
                                                form.setData(
                                                    'amount',
                                                    e.target.value,
                                                )
                                            }
                                            placeholder="Masukkan jumlah pembayaran"
                                            required
                                        />
                                        {form.errors.amount && (
                                            <InputError
                                                message={form.errors.amount}
                                            />
                                        )}

                                        {/* Quick Amount Buttons */}
                                        <div className="mt-2 flex flex-wrap gap-2">
                                            {nextDue && (
                                                <Button
                                                    type="button"
                                                    variant="outline"
                                                    size="sm"
                                                    onClick={() =>
                                                        handleQuickAmount(
                                                            nextDue.total_amount -
                                                                nextDue.paid_total,
                                                        )
                                                    }
                                                >
                                                    Angsuran Berikutnya
                                                </Button>
                                            )}
                                            {totalOverdue > 0 && (
                                                <Button
                                                    type="button"
                                                    variant="outline"
                                                    size="sm"
                                                    onClick={() => {
                                                        const overdueTotal =
                                                            pendingInstallments
                                                                .filter(
                                                                    (i) =>
                                                                        new Date(
                                                                            i.due_date,
                                                                        ) <
                                                                            new Date() &&
                                                                        i.status !==
                                                                            'paid',
                                                                )
                                                                .reduce(
                                                                    (sum, i) =>
                                                                        sum +
                                                                        (i.total_amount -
                                                                            i.paid_total),
                                                                    0,
                                                                );
                                                        handleQuickAmount(
                                                            overdueTotal,
                                                        );
                                                    }}
                                                >
                                                    Total Telat Bayar
                                                </Button>
                                            )}
                                            <Button
                                                type="button"
                                                variant="outline"
                                                size="sm"
                                                onClick={() =>
                                                    handleQuickAmount(
                                                        financing.installment_amount,
                                                    )
                                                }
                                            >
                                                Angsuran Harian
                                            </Button>
                                        </div>
                                    </div>

                                    {/* Payment Method */}
                                    <div className="space-y-2">
                                        <Label htmlFor="payment_method">
                                            Metode Pembayaran{' '}
                                            <span className="text-destructive">
                                                *
                                            </span>
                                        </Label>
                                        <Select
                                            value={form.data.payment_method}
                                            onValueChange={(
                                                value: PaymentMethod,
                                            ) =>
                                                form.setData(
                                                    'payment_method',
                                                    value,
                                                )
                                            }
                                            required
                                        >
                                            <SelectTrigger>
                                                <SelectValue placeholder="Pilih metode pembayaran" />
                                            </SelectTrigger>
                                            <SelectContent>
                                                <SelectGroup>
                                                    <SelectLabel>
                                                        Metode Pembayaran
                                                    </SelectLabel>
                                                    <SelectItem value="cash">
                                                        Tunai
                                                    </SelectItem>
                                                    <SelectItem value="transfer">
                                                        Transfer
                                                    </SelectItem>
                                                    <SelectItem value="auto_debit">
                                                        Auto Debit
                                                    </SelectItem>
                                                </SelectGroup>
                                            </SelectContent>
                                        </Select>
                                        {form.errors.payment_method && (
                                            <InputError
                                                message={
                                                    form.errors.payment_method
                                                }
                                            />
                                        )}
                                    </div>

                                    {/* Payment Date */}
                                    <div className="space-y-2">
                                        <Label htmlFor="payment_date">
                                            Tanggal Pembayaran{' '}
                                            <span className="text-destructive">
                                                *
                                            </span>
                                        </Label>
                                        <Input
                                            id="payment_date"
                                            type="date"
                                            value={form.data.payment_date}
                                            onChange={(e) =>
                                                form.setData(
                                                    'payment_date',
                                                    e.target.value,
                                                )
                                            }
                                            required
                                        />
                                        {form.errors.payment_date && (
                                            <InputError
                                                message={
                                                    form.errors.payment_date
                                                }
                                            />
                                        )}
                                    </div>

                                    {/* Notes */}
                                    <div className="space-y-2">
                                        <Label htmlFor="notes">
                                            Catatan (Opsional)
                                        </Label>
                                        <textarea
                                            id="notes"
                                            value={form.data.notes}
                                            onChange={(e) =>
                                                form.setData(
                                                    'notes',
                                                    e.target.value,
                                                )
                                            }
                                            placeholder="Catatan pembayaran..."
                                            rows={2}
                                            className="flex min-h-15 w-full rounded-md border border-input bg-transparent px-3 py-2 text-sm shadow-sm placeholder:text-muted-foreground focus-visible:ring-1 focus-visible:ring-ring focus-visible:outline-none disabled:cursor-not-allowed disabled:opacity-50"
                                        />
                                        {form.errors.notes && (
                                            <InputError
                                                message={form.errors.notes}
                                            />
                                        )}
                                    </div>
                                </CardContent>
                            </Card>

                            {/* Submit Button */}
                            <div className="flex justify-end gap-2">
                                <Button
                                    type="button"
                                    variant="outline"
                                    asChild
                                    disabled={form.processing}
                                >
                                    <Link
                                        href={
                                            financingsRoutes.show({
                                                financing: financing.id,
                                            }).url
                                        }
                                    >
                                        Batal
                                    </Link>
                                </Button>
                                <Button
                                    type="submit"
                                    disabled={form.processing}
                                >
                                    {form.processing
                                        ? 'Memproses...'
                                        : 'Catat Pembayaran'}
                                </Button>
                            </div>
                        </form>
                    </div>

                    {/* Allocation Preview Column */}
                    <div className="flex flex-col gap-2 lg:col-span-1">
                        {/* Financing Info */}
                        <Card>
                            <CardHeader>
                                <CardTitle>Informasi Pembiayaan</CardTitle>
                            </CardHeader>
                            <CardContent className="space-y-3">
                                <div className="flex justify-between">
                                    <span className="text-muted-foreground">
                                        Saldo Outstanding
                                    </span>
                                    <span className="text-lg font-bold">
                                        {formatIDR(financing.total_balance)}
                                    </span>
                                </div>
                                <div className="flex justify-between">
                                    <span className="text-muted-foreground">
                                        Jatuh Tempo Berikutnya
                                    </span>
                                    <span className="font-medium">
                                        {nextDue
                                            ? formatDateID(nextDue.due_date)
                                            : '-'}
                                    </span>
                                </div>
                                <div className="flex justify-between">
                                    <span className="text-muted-foreground">
                                        Angsuran Tertunggak
                                    </span>
                                    <span className="font-medium">
                                        {pendingInstallments.length} angsuran
                                        {totalOverdue > 0 && (
                                            <span className="ml-2 text-destructive">
                                                ({totalOverdue} telat bayar)
                                            </span>
                                        )}
                                    </span>
                                </div>
                                <div className="flex justify-between">
                                    <span className="text-muted-foreground">
                                        Kolektibilitas
                                    </span>
                                    <StatusBadge
                                        type="collectibility"
                                        status={
                                            financing.collectibility_score?.toString() ||
                                            '1'
                                        }
                                    />
                                </div>
                            </CardContent>
                        </Card>

                        <Card className="sticky top-4">
                            <CardHeader>
                                <CardTitle className="flex items-center gap-2">
                                    <Calculator className="h-5 w-5" />
                                    Alokasi Pembayaran
                                </CardTitle>
                            </CardHeader>
                            <CardContent className="space-y-4">
                                {/* Allocation Mode Toggle */}
                                <div className="flex gap-2">
                                    <Button
                                        type="button"
                                        variant={
                                            allocationMode === 'auto'
                                                ? 'default'
                                                : 'outline'
                                        }
                                        className="flex-1"
                                        size="sm"
                                        onClick={() => {
                                            setAllocationMode('auto');
                                            form.setData('installment_ids', []);
                                        }}
                                    >
                                        Otomatis
                                    </Button>
                                    <Button
                                        type="button"
                                        variant={
                                            allocationMode === 'manual'
                                                ? 'default'
                                                : 'outline'
                                        }
                                        className="flex-1"
                                        size="sm"
                                        onClick={() => {
                                            setAllocationMode('manual');
                                            // Set amount to 0 when switching to manual mode
                                            form.setData('amount', '0');
                                        }}
                                    >
                                        Manual
                                    </Button>
                                </div>

                                {/* Auto Allocation Preview */}
                                {allocationMode === 'auto' && (
                                    <div className="space-y-3">
                                        {form.data.amount &&
                                        Number(form.data.amount) > 0 ? (
                                            <>
                                                {autoAllocation ? (
                                                    <>
                                                        <div className="rounded-lg bg-primary/10 p-3">
                                                            <div className="mb-2 text-sm font-medium">
                                                                Alokasi Otomatis
                                                                (FIFO)
                                                            </div>
                                                            <div className="space-y-2 text-xs">
                                                                {autoAllocation.allocation.map(
                                                                    (item) => (
                                                                        <div
                                                                            key={
                                                                                item
                                                                                    .installment
                                                                                    .id
                                                                            }
                                                                            className="flex justify-between"
                                                                        >
                                                                            <span>
                                                                                Angsuran
                                                                                #{' '}
                                                                                {
                                                                                    item
                                                                                        .installment
                                                                                        .sequence_number
                                                                                }
                                                                            </span>
                                                                            <span className="font-medium">
                                                                                {formatIDR(
                                                                                    item.total,
                                                                                )}
                                                                            </span>
                                                                        </div>
                                                                    ),
                                                                )}
                                                            </div>
                                                        </div>

                                                        {autoAllocation.remaining >
                                                            0 && (
                                                            <div className="rounded-lg bg-yellow-100 p-3 dark:bg-yellow-900/20">
                                                                <div className="text-sm font-medium text-yellow-800 dark:text-yellow-200">
                                                                    Sisa:{' '}
                                                                    {formatIDR(
                                                                        autoAllocation.remaining,
                                                                    )}
                                                                </div>
                                                                <div className="text-xs text-yellow-700 dark:text-yellow-300">
                                                                    Akan
                                                                    dialokasikan
                                                                    ke angsuran
                                                                    berikutnya
                                                                </div>
                                                            </div>
                                                        )}

                                                        <div className="text-center text-sm text-muted-foreground">
                                                            Sistem akan membayar
                                                            angsuran tertua
                                                            terlebih dahulu
                                                        </div>
                                                    </>
                                                ) : (
                                                    <div className="text-center text-sm text-muted-foreground">
                                                        Masukkan jumlah
                                                        pembayaran untuk melihat
                                                        alokasi
                                                    </div>
                                                )}
                                            </>
                                        ) : (
                                            <div className="text-center text-sm text-muted-foreground">
                                                Masukkan jumlah pembayaran
                                            </div>
                                        )}
                                    </div>
                                )}

                                {/* Manual Allocation */}
                                {allocationMode === 'manual' && (
                                    <div className="space-y-3">
                                        <div className="text-sm">
                                            Pilih angsuran yang ingin dibayar:
                                        </div>

                                        <div className="max-h-80 space-y-2 overflow-y-auto">
                                            {pendingInstallments.map(
                                                (installment) => {
                                                    const isSelected =
                                                        form.data.installment_ids.includes(
                                                            installment.id,
                                                        );
                                                    const outstanding =
                                                        installment.total_amount -
                                                        installment.paid_total;

                                                    return (
                                                        <div
                                                            key={installment.id}
                                                            className="space-y-1 rounded-lg border p-3"
                                                        >
                                                            <div className="flex items-start gap-2">
                                                                <Checkbox
                                                                    id={`installment-${installment.id}`}
                                                                    checked={
                                                                        isSelected
                                                                    }
                                                                    onCheckedChange={() =>
                                                                        toggleInstallment(
                                                                            installment.id,
                                                                        )
                                                                    }
                                                                />
                                                                <label
                                                                    htmlFor={`installment-${installment.id}`}
                                                                    className="flex-1 cursor-pointer"
                                                                >
                                                                    <div className="flex justify-between text-sm font-medium">
                                                                        <span>
                                                                            Angsuran
                                                                            #
                                                                            {
                                                                                installment.sequence_number
                                                                            }
                                                                        </span>
                                                                        <span>
                                                                            {formatIDR(
                                                                                outstanding,
                                                                            )}
                                                                        </span>
                                                                    </div>
                                                                    <div className="text-xs text-muted-foreground">
                                                                        {formatDateID(
                                                                            installment.due_date,
                                                                        )}
                                                                    </div>
                                                                </label>
                                                            </div>
                                                        </div>
                                                    );
                                                },
                                            )}
                                        </div>

                                        {form.data.installment_ids.length >
                                            0 && (
                                            <div className="space-y-1 rounded-lg bg-primary/10 p-3">
                                                <div className="flex justify-between text-sm">
                                                    <span className="font-medium">
                                                        Total Terpilih:
                                                    </span>
                                                    <span className="font-bold">
                                                        {formatIDR(manualTotal)}
                                                    </span>
                                                </div>
                                                {Math.abs(
                                                    Number(form.data.amount) -
                                                        manualTotal,
                                                ) > 100 && (
                                                    <div className="text-xs text-destructive">
                                                        Jumlah tidak sesuai
                                                        dengan pembayaran
                                                    </div>
                                                )}
                                            </div>
                                        )}
                                    </div>
                                )}
                            </CardContent>
                        </Card>
                    </div>
                </div>
            </div>
        </AppLayout>
    );
}
