import { Head, Link } from '@inertiajs/react';
import {
    Plus,
    Repeat,
    Target,
    UserRound,
    Users,
    UsersRound,
} from 'lucide-react';
import EmptyState from '@/components/member/empty-state';
import MemberPage from '@/components/member/member-page';
import StatusBadge from '@/components/member/status-badge';
import { formatNaira } from '@/lib/format';
import { cn } from '@/lib/utils';
import { create, index, show } from '@/routes/admin/groups';

export type GroupSummary = {
    id: number;
    name: string;
    description: string | null;
    leader: { id: number; name: string } | null;
    contributionAmount: number;
    contributionFrequency: string;
    contributionFrequencyLabel: string;
    savingsTarget: number | null;
    isActive: boolean;
};

export default function GroupsIndex({
    groups,
    canManage,
}: {
    groups: (GroupSummary & { membersCount: number })[];
    canManage: boolean;
}) {
    const newButton = canManage ? (
        <Link
            href={create()}
            className="inline-flex items-center gap-1.5 rounded-full bg-brand px-3.5 py-1.5 text-sm font-semibold text-white hover:bg-brand-hover"
        >
            <Plus className="size-4" />
            New group
        </Link>
    ) : undefined;

    return (
        <>
            <Head title="Smart Save groups" />

            <MemberPage
                title="Smart Save groups"
                description="Members who save the same amount on the same schedule, with a leader and their own rules."
                actions={newButton}
            >
                {groups.length === 0 ? (
                    <EmptyState
                        icon={UsersRound}
                        title="No groups yet"
                        description="Create a group, choose its leader and contribution, then add members."
                        action={newButton}
                    />
                ) : (
                    <ul className="grid grid-cols-1 gap-3 md:grid-cols-2 xl:grid-cols-3">
                        {groups.map((group) => (
                            <li key={group.id}>
                                <Link
                                    href={show(group.id)}
                                    className={cn(
                                        'flex h-full flex-col gap-3 rounded-landing-lg border border-surface-200 bg-white p-4 shadow-landing-sm transition-colors hover:border-brand/40',
                                        !group.isActive && 'opacity-70',
                                    )}
                                >
                                    <div className="flex items-start gap-3">
                                        <span className="flex size-10 shrink-0 items-center justify-center rounded-landing bg-brand-faint text-brand">
                                            <UsersRound className="size-5" />
                                        </span>
                                        <span className="min-w-0 flex-1">
                                            <span className="block truncate font-semibold text-ink">
                                                {group.name}
                                            </span>
                                            <span className="block truncate text-xs text-ink-400">
                                                {group.description ??
                                                    'Smart Save group'}
                                            </span>
                                        </span>
                                        {!group.isActive && (
                                            <StatusBadge tone="neutral">
                                                Inactive
                                            </StatusBadge>
                                        )}
                                    </div>
                                    <dl className="grid grid-cols-2 gap-2 border-t border-surface-100 pt-3 text-xs">
                                        <Fact icon={Repeat} term="Contribution">
                                            {formatNaira(
                                                group.contributionAmount,
                                            )}{' '}
                                            {group.contributionFrequencyLabel.toLowerCase()}
                                        </Fact>
                                        <Fact icon={Users} term="Members">
                                            {group.membersCount}
                                        </Fact>
                                        <Fact icon={UserRound} term="Leader">
                                            {group.leader?.name ?? 'Not set'}
                                        </Fact>
                                        <Fact icon={Target} term="Target">
                                            {group.savingsTarget
                                                ? formatNaira(
                                                      group.savingsTarget,
                                                  )
                                                : '—'}
                                        </Fact>
                                    </dl>
                                </Link>
                            </li>
                        ))}
                    </ul>
                )}
            </MemberPage>
        </>
    );
}

function Fact({
    icon: Icon,
    term,
    children,
}: {
    icon: typeof Users;
    term: string;
    children: React.ReactNode;
}) {
    return (
        <div className="min-w-0">
            <dt className="flex items-center gap-1 text-ink-400">
                <Icon className="size-3" />
                {term}
            </dt>
            <dd className="mt-0.5 truncate font-semibold text-ink-700">
                {children}
            </dd>
        </div>
    );
}

GroupsIndex.layout = {
    breadcrumbs: [{ title: 'Smart Save groups', href: index() }],
};
