/* eslint-disable @typescript-eslint/no-explicit-any */
import { render, screen } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
import TestimonialForm from '../Form';

// ── Mocks ─────────────────────────────────────────────────────────────────────

const mockPost = vi.fn();
const mockPut = vi.fn();
const mockSetData = vi.fn();

vi.mock('@inertiajs/react', () => ({
    Head: ({ title }: { title: string }) => <title>{title}</title>,
    Link: ({ href, children, ...rest }: any) => (
        <a href={href} {...rest}>
            {children}
        </a>
    ),
    router: {
        get: vi.fn(),
        post: vi.fn(),
        put: vi.fn(),
        patch: vi.fn(),
        delete: vi.fn(),
        visit: vi.fn(),
    },
    usePage: vi.fn(() => ({
        props: {
            auth: { adminRole: 'super_admin', adminPermissions: [] },
            flash: {},
            errors: {},
        },
    })),
    useForm: vi.fn((defaults: any) => ({
        data: { ...defaults },
        setData: mockSetData,
        post: mockPost,
        put: mockPut,
        patch: vi.fn(),
        delete: vi.fn(),
        processing: false,
        errors: {},
        reset: vi.fn(),
        transform: vi.fn().mockReturnThis(),
    })),
}));

vi.mock('@/Layouts/AdminLayout', () => ({
    default: ({ children, title }: any) => (
        <div data-testid="admin-layout" data-title={title}>
            {children}
        </div>
    ),
}));

vi.mock('@/lib/api', () => ({
    api: {
        get: vi.fn(),
        post: vi.fn(),
        put: vi.fn(),
        patch: vi.fn(),
        delete: vi.fn(),
    },
}));

vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } }));

vi.mock('@/components/admin/PageHeader', () => ({
    default: ({ title }: any) => <h2>{title}</h2>,
}));

// ── Fixtures ──────────────────────────────────────────────────────────────────

const EXISTING_TESTIMONIAL = {
    id: 'dd000001-0000-0000-0000-000000000001',
    quote: 'We moved off Excel in one weekend.',
    name: 'Grace Njeri',
    role: 'CEO',
    company: 'Mwanga Microfinance',
    location: 'Nairobi, Kenya',
    metric: '+30% repayment processing',
    published_at: '2025-06-01T10:00',
    status: 'published',
    avatar_url: null,
    created_at: '2025-05-28T00:00:00Z',
};

// ── Tests ─────────────────────────────────────────────────────────────────────

describe('Admin/Testimonials/Form — create mode', () => {
    it('renders inside AdminLayout without crashing', () => {
        render(<TestimonialForm />);
        expect(screen.getByTestId('admin-layout')).toBeInTheDocument();
    });

    it('sets AdminLayout title to "New Testimonial" in create mode', () => {
        render(<TestimonialForm />);
        expect(screen.getByTestId('admin-layout')).toHaveAttribute(
            'data-title',
            'New Testimonial',
        );
    });

    it('shows "New Testimonial" heading in create mode', () => {
        render(<TestimonialForm />);
        expect(
            screen.getByRole('heading', { name: 'New Testimonial' }),
        ).toBeInTheDocument();
    });

    it('renders a blank name input in create mode', () => {
        render(<TestimonialForm />);
        const nameInput = screen.getByPlaceholderText('Jane Doe');
        expect(nameInput).toBeInTheDocument();
        expect(nameInput).toHaveValue('');
    });

    it('renders a blank quote textarea in create mode', () => {
        render(<TestimonialForm />);
        expect(
            screen.getByPlaceholderText(/what did the customer say/i),
        ).toHaveValue('');
    });

    it('renders a publish date input with helper text', () => {
        render(<TestimonialForm />);
        expect(
            screen.getByText(/Leave blank to save as draft/i),
        ).toBeInTheDocument();
    });

    it('renders a "Save draft" submit button when no publish date is set', () => {
        render(<TestimonialForm />);
        expect(
            screen.getByRole('button', { name: /save draft/i }),
        ).toBeInTheDocument();
    });

    it('renders a Cancel link back to /admin/testimonials', () => {
        render(<TestimonialForm />);
        expect(screen.getByRole('link', { name: /cancel/i })).toHaveAttribute(
            'href',
            '/admin/testimonials',
        );
    });
});

describe('Admin/Testimonials/Form — edit mode', () => {
    it('sets AdminLayout title to "Edit Testimonial" in edit mode', () => {
        render(<TestimonialForm testimonial={EXISTING_TESTIMONIAL as any} />);
        expect(screen.getByTestId('admin-layout')).toHaveAttribute(
            'data-title',
            'Edit Testimonial',
        );
    });

    it('pre-fills the name input with the existing name', () => {
        render(<TestimonialForm testimonial={EXISTING_TESTIMONIAL as any} />);
        expect(screen.getByPlaceholderText('Jane Doe')).toHaveValue(
            'Grace Njeri',
        );
    });

    it('pre-fills the quote textarea with the existing quote', () => {
        render(<TestimonialForm testimonial={EXISTING_TESTIMONIAL as any} />);
        expect(
            screen.getByPlaceholderText(/what did the customer say/i),
        ).toHaveValue('We moved off Excel in one weekend.');
    });

    it('shows "Update" submit button in edit mode when publish date is set', () => {
        render(<TestimonialForm testimonial={EXISTING_TESTIMONIAL as any} />);
        expect(
            screen.getByRole('button', { name: /update/i }),
        ).toBeInTheDocument();
    });
});
