'use client';

/**
 * CTAs deep-link: sincronizar pack (hub Conectar), baixar contexto, aplicar/revisar sugestões e guia do pack.
 * Epic 4 / Sync L2 C2: `emphasizeApplyFromSync` destaca «Aplicar do handbook» (+ diff N pendentes).
 */

import React from 'react';
import Link from 'next/link';
import { Button, Space } from 'antd';
import { BookOpen, HardDriveDownload, Lightbulb, RefreshCw } from 'lucide-react';
import { ICON_SIZE_MD } from '@/components/icons';
import { projetosProjetoCanonical } from '@/lib/routes/projetosProjetoCanonical';
import { tipologicoAplicarDoHandbookLabel } from './tipologicoEmptyCopy';
import { useCursorSugestoesPendentesCount } from './useCursorSugestoesPendentesCount';

export type ProjetoTipologicoSyncCtasProps = {
    projetoId: string;
    /** Tamanho Ant Design dos botões. */
    size?: 'small' | 'middle' | 'large';
    /** `primary` destaca «Sincronizar pack»; ignorado se `emphasizeApplyFromSync`. */
    emphasizeSync?: boolean;
    /**
     * Destaca «Aplicar do handbook» (deep-link sugestões Cursor) como primário
     * e mantém «Sincronizar pack» como secundário.
     */
    emphasizeApplyFromSync?: boolean;
    /**
     * Contagem pendente pré-resolvida (empty state). Se omitida e
     * `emphasizeApplyFromSync`, o CTA consulta a mesma query de sugestoes-cursor.
     */
    pendentesCount?: number | null;
    /** Exibe link para o guia Cursor Pack (default true). */
    showGuiaPack?: boolean;
    /** Exibe CTA «Baixar contexto» → guia #baixar-contexto (CUR-504, default true). */
    showBaixarContexto?: boolean;
    className?: string;
    'data-testid'?: string;
};

export function ProjetoTipologicoSyncCtas({
    projetoId,
    size = 'middle',
    emphasizeSync = false,
    emphasizeApplyFromSync = false,
    pendentesCount: pendentesCountProp,
    showGuiaPack = true,
    showBaixarContexto = true,
    className,
    'data-testid': dataTestId = 'projeto-tipologico-sync-ctas',
}: ProjetoTipologicoSyncCtasProps) {
    const shouldFetchPendentes = emphasizeApplyFromSync && pendentesCountProp === undefined;
    const { pendentesCount: pendentesFromQuery } = useCursorSugestoesPendentesCount(
        projetoId,
        shouldFetchPendentes,
    );
    const pendentesCount =
        pendentesCountProp !== undefined ? pendentesCountProp : pendentesFromQuery;

    if (!projetoId) return null;

    const guiaBaixarHref = `${projetosProjetoCanonical.desenvolvimentoGuiaCursorPack(projetoId)}#baixar-contexto`;
    const sugestoesHref = projetosProjetoCanonical.desenvolvimentoSugestoesCursor(projetoId);
    const syncIsPrimary = emphasizeSync && !emphasizeApplyFromSync;
    const aplicarLabel = tipologicoAplicarDoHandbookLabel(pendentesCount);

    return (
        <Space wrap size={8} className={className} data-testid={dataTestId}>
            {emphasizeApplyFromSync ? (
                <Link href={sugestoesHref}>
                    <Button
                        type="primary"
                        size={size}
                        icon={<Lightbulb size={ICON_SIZE_MD} aria-hidden />}
                        data-testid={`${dataTestId}-aplicar-sync`}
                    >
                        {aplicarLabel}
                    </Button>
                </Link>
            ) : null}
            <Link href={projetosProjetoCanonical.projetoConectar(projetoId)}>
                <Button
                    type={syncIsPrimary ? 'primary' : 'default'}
                    size={size}
                    icon={<RefreshCw size={ICON_SIZE_MD} aria-hidden />}
                    data-testid={`${dataTestId}-sincronizar`}
                >
                    Sincronizar pack
                </Button>
            </Link>
            {showBaixarContexto ? (
                <Link href={guiaBaixarHref}>
                    <Button
                        type="default"
                        size={size}
                        icon={<HardDriveDownload size={ICON_SIZE_MD} aria-hidden />}
                        data-testid={`${dataTestId}-baixar-contexto`}
                    >
                        Baixar contexto
                    </Button>
                </Link>
            ) : null}
            {!emphasizeApplyFromSync ? (
                <Link href={sugestoesHref}>
                    <Button
                        type="link"
                        size={size}
                        icon={<Lightbulb size={ICON_SIZE_MD} aria-hidden />}
                        data-testid={`${dataTestId}-sugestoes`}
                    >
                        Revisar sugestões
                    </Button>
                </Link>
            ) : null}
            {showGuiaPack ? (
                <Link href={projetosProjetoCanonical.desenvolvimentoGuiaCursorPack(projetoId)}>
                    <Button
                        type="link"
                        size={size}
                        icon={<BookOpen size={ICON_SIZE_MD} aria-hidden />}
                        data-testid={`${dataTestId}-guia`}
                    >
                        Guia do pack
                    </Button>
                </Link>
            ) : null}
        </Space>
    );
}
