import { usePage } from '@inertiajs/react';
import { Badge } from '@/components/ui/badge';
import { cn } from '@/lib/utils';

/** Whether a record is live on the public site. */
export function PublishedBadge({ published }: { published: boolean }) {
    return published ? (
        <Badge
            variant="outline"
            className="border-emerald-600/30 text-emerald-700 dark:text-emerald-400"
        >
            Yayında
        </Badge>
    ) : (
        <Badge variant="secondary">Taslak</Badge>
    );
}

/** Where a blog post stands (PostController::status()). */
export type PostStatus = 'live' | 'scheduled' | 'draft';

/**
 * State of a blog post. Besides live and draft a post can be scheduled:
 * published, but waiting for its date.
 */
export function PostStatusBadge({ status }: { status: PostStatus }) {
    if (status === 'scheduled') {
        return (
            <Badge
                variant="outline"
                className="border-sky-600/30 text-sky-700 dark:text-sky-400"
            >
                Zamanlandı
            </Badge>
        );
    }

    return <PublishedBadge published={status === 'live'} />;
}

/** Marks the languages a record has no (complete) translation in. */
export function MissingTranslations({ locales }: { locales: string[] }) {
    const all = usePage().props.locales;

    if (locales.length === 0) {
        return null;
    }

    return (
        <span className="inline-flex flex-wrap gap-1">
            {locales.map((code) => {
                const locale = all.find((item) => item.code === code);

                return (
                    <Badge
                        key={code}
                        variant="outline"
                        title={`${locale?.name ?? code} çevirisi eksik`}
                        className="border-amber-500/40 text-amber-700 dark:text-amber-400"
                    >
                        {locale?.short ?? code} eksik
                    </Badge>
                );
            })}
        </span>
    );
}

/**
 * Marks a request sent through the short form in front of WhatsApp: the
 * visitor left a name and a phone number and expects the chat.
 */
export function WhatsAppBadge({ className }: { className?: string }) {
    return (
        <Badge
            variant="outline"
            title="WhatsApp formundan geldi: ziyaretçi sohbet bekliyor"
            className={cn(
                'border-emerald-600/30 text-emerald-700 dark:text-emerald-400',
                className,
            )}
        >
            WhatsApp
        </Badge>
    );
}

const LEAD_STATUS_STYLES: Record<string, string> = {
    new: 'border-transparent bg-blue-600 text-white',
    contacted:
        'border-amber-500/40 bg-transparent text-amber-700 dark:text-amber-400',
    closed: 'border-transparent bg-secondary text-secondary-foreground',
};

/** Status of a lead (yeni / arandı / kapandı), labelled by the server. */
export function LeadStatusBadge({
    status,
    label,
}: {
    status: string;
    label: string;
}) {
    return <Badge className={cn(LEAD_STATUS_STYLES[status])}>{label}</Badge>;
}
