import { Button } from '@/components/ui/button';
import { cn } from '@/lib/utils';
import { Link } from '@inertiajs/react';
import { ColumnDef } from '@tanstack/react-table';
import { ArrowUpDown } from 'lucide-react';
import { formatTnd, occupancyTextColor, type KpiPropertyRow } from './kpi-types';

type T = (ns: string, key: string, fallback?: string) => string;

function sortableHeader(label: string) {
    // eslint-disable-next-line react/display-name
    return ({ column }: { column: { toggleSorting: (desc?: boolean) => void; getIsSorted: () => false | 'asc' | 'desc' } }) => (
        <Button variant="ghost" className="-ml-3 h-8 px-2" onClick={() => column.toggleSorting(column.getIsSorted() === 'asc')}>
            {label}
            <ArrowUpDown className="ml-2 h-3.5 w-3.5" />
        </Button>
    );
}

export function getKpiColumns(t: T, locale: string): ColumnDef<KpiPropertyRow>[] {
    return [
        {
            accessorKey: 'name',
            header: sortableHeader(t('properties.kpi', 'col_property')),
            cell: ({ row }) => (
                <Link href={route('logements.show', { id: row.original.id })} className="text-secondary font-medium hover:underline">
                    {row.original.name ?? '—'}
                </Link>
            ),
        },
        {
            accessorKey: 'location',
            header: t('properties.kpi', 'col_location'),
            cell: ({ row }) => <span className="text-muted-foreground text-sm">{row.original.location ?? '—'}</span>,
        },
        {
            accessorKey: 'owner_name',
            header: t('properties.kpi', 'col_owner'),
            cell: ({ row }) => <span className="text-muted-foreground text-sm">{row.original.owner_name ?? '—'}</span>,
        },
        {
            accessorKey: 'revenue',
            header: sortableHeader(t('properties.kpi', 'col_revenue')),
            cell: ({ row }) => <span className="font-semibold tabular-nums">{formatTnd(row.original.revenue, locale)}</span>,
        },
        {
            accessorKey: 'occupancy_rate',
            header: sortableHeader(t('properties.kpi', 'col_occupancy')),
            cell: ({ row }) => (
                <span className={cn('font-semibold tabular-nums', occupancyTextColor(row.original.occupancy_rate))}>
                    {row.original.occupancy_rate}%
                </span>
            ),
        },
        {
            accessorKey: 'nights_booked',
            header: sortableHeader(t('properties.kpi', 'col_nights')),
            cell: ({ row }) => <span className="tabular-nums">{row.original.nights_booked}</span>,
        },
        {
            accessorKey: 'reservations_count',
            header: sortableHeader(t('properties.kpi', 'col_reservations')),
            cell: ({ row }) => <span className="tabular-nums">{row.original.reservations_count}</span>,
        },
        {
            accessorKey: 'avg_price_per_night',
            header: t('properties.kpi', 'col_avg_price'),
            cell: ({ row }) => <span className="tabular-nums">{formatTnd(row.original.avg_price_per_night, locale)}</span>,
        },
        {
            accessorKey: 'avg_length_of_stay',
            header: t('properties.kpi', 'col_avg_stay'),
            cell: ({ row }) => (
                <span className="tabular-nums">
                    {row.original.avg_length_of_stay} {t('properties.kpi', 'unit_nights')}
                </span>
            ),
        },
    ];
}
