'use client';

import React from 'react';
import Link from 'next/link';
import { Button } from 'antd';
import {
    getLaravelApiNextAction,
    getLaravelApiNextActionCtaLabel,
} from '@/lib/api/laravelApiErrorMessage';

export type ForbiddenNextActionCtaProps = {
    error: unknown;
    /** Classes / estilo do wrapper (ex.: margem no Alert). */
    style?: React.CSSProperties;
    'data-testid'?: string;
};

/**
 * CTA navegável a partir de `next_action.href` num 403 (UX-FL-G001).
 * Sem href válido, não renderiza nada.
 */
export function ForbiddenNextActionCta({
    error,
    style,
    'data-testid': dataTestId = 'api-forbidden-next-action',
}: ForbiddenNextActionCtaProps) {
    const action = getLaravelApiNextAction(error);
    if (!action) {
        return null;
    }

    return (
        <span style={style}>
            <Link href={action.href} data-testid={dataTestId}>
                <Button type="primary" size="small">
                    {getLaravelApiNextActionCtaLabel(action)}
                </Button>
            </Link>
        </span>
    );
}
