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

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: { user: null },
            flash: {},
            errors: {},
            currency: {
                current: 'USD',
                base: 'USD',
                available: [
                    {
                        code: 'USD',
                        name: 'US Dollar',
                        symbol: '$',
                        rate: 1,
                        decimals: 2,
                    },
                ],
            },
            ziggy: {
                url: 'http://localhost',
                port: null,
                defaults: [],
                routes: {},
            },
        },
    })),
    useForm: vi.fn(() => ({
        data: {},
        setData: vi.fn(),
        post: vi.fn(),
        processing: false,
        errors: {},
        reset: vi.fn(),
    })),
}));

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

vi.mock('@/components/illustrations', () => ({
    DatabaseIsolationIllustration: () => (
        <svg data-testid="db-isolation-illus" />
    ),
    EastAfricaMapIllustration: () => <svg data-testid="east-africa-illus" />,
    IsometricLoanIllustration: () => <svg data-testid="isometric-loan-illus" />,
    OnboardingFlowIllustration: () => <svg data-testid="onboarding-illus" />,
}));

vi.mock('framer-motion', () => ({
    motion: {
        div: ({ children, ...rest }: any) => <div {...rest}>{children}</div>,
        h1: ({ children, ...rest }: any) => <h1 {...rest}>{children}</h1>,
        p: ({ children, ...rest }: any) => <p {...rest}>{children}</p>,
    },
    useInView: () => true,
    AnimatePresence: ({ children }: any) => <>{children}</>,
}));

vi.stubGlobal('route', (name: string) => `/${name}`);

const plans = [
    {
        slug: 'trial',
        name: 'Free Trial',
        description: 'Explore everything with no commitment.',
        price_monthly: 0,
        price_yearly: 0,
        trial_days: 30,
    },
    {
        slug: 'starter',
        name: 'Starter',
        description: 'For small SACCOs and community lenders.',
        price_monthly: 49,
        price_yearly: 490,
        trial_days: 0,
    },
    {
        slug: 'professional',
        name: 'Professional',
        description: 'For growing MFIs.',
        price_monthly: 149,
        price_yearly: 1490,
        trial_days: 0,
    },
    {
        slug: 'enterprise',
        name: 'Enterprise',
        description: 'For regulated institutions.',
        price_monthly: 0,
        price_yearly: 0,
        trial_days: 0,
    },
];

describe('Platform Home page', () => {
    it('renders inside PlatformLayout', () => {
        render(<Home plans={plans} />);
        expect(screen.getByTestId('platform-layout')).toBeInTheDocument();
    });

    it('renders the hero heading about East Africa', () => {
        render(<Home plans={plans} />);
        expect(screen.getAllByText(/east africa/i).length).toBeGreaterThan(0);
    });

    it('renders "Start free trial" CTA link', () => {
        render(<Home plans={plans} />);
        const links = screen.getAllByText(/start free trial/i);
        expect(links.length).toBeGreaterThan(0);
    });

    it('renders "See all features" link', () => {
        render(<Home plans={plans} />);
        expect(screen.getByText('See all features')).toBeInTheDocument();
    });

    it('renders the "Trusted in" strip with Tanzania', () => {
        render(<Home plans={plans} />);
        expect(screen.getByText('Tanzania')).toBeInTheDocument();
    });

    it('renders the Loan Management module card', () => {
        render(<Home plans={plans} />);
        expect(screen.getAllByText('Loan Management').length).toBeGreaterThan(
            0,
        );
    });

    it('renders pricing tier names', () => {
        render(<Home plans={plans} />);
        expect(screen.getByText('Starter')).toBeInTheDocument();
        expect(screen.getByText('Professional')).toBeInTheDocument();
    });

    it('renders plan prices in the display currency', () => {
        render(<Home plans={plans} />);
        expect(screen.getByText('$49')).toBeInTheDocument();
        expect(screen.getByText('$149')).toBeInTheDocument();
        expect(screen.getByText('30 days')).toBeInTheDocument();
        expect(screen.getByText('Custom')).toBeInTheDocument();
    });

    it('renders testimonial from Ibrahim Mwangi', () => {
        render(<Home plans={plans} />);
        expect(screen.getByText('Ibrahim Mwangi')).toBeInTheDocument();
    });

    it('renders "Compare all plans" link', () => {
        render(<Home plans={plans} />);
        expect(screen.getByText(/compare all plans/i)).toBeInTheDocument();
    });

    it('renders the "Talk to sales" CTA', () => {
        render(<Home plans={plans} />);
        expect(screen.getByText('Talk to sales')).toBeInTheDocument();
    });
});
