/* eslint-disable @typescript-eslint/no-explicit-any */
import { render, screen } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import AppSettingsComponent from '../Settings';
const AppSettings = AppSettingsComponent as any;

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: { id: '1', name: 'Test User', email: 'test@example.com' },
            },
            flash: {},
            errors: {},
            ziggy: {
                url: 'http://localhost',
                port: null,
                defaults: [],
                routes: {},
            },
        },
    })),
    useForm: vi.fn((defaults: any) => ({
        data: defaults ?? {},
        setData: vi.fn(),
        patch: vi.fn(),
        post: vi.fn(),
        processing: false,
        errors: {},
        isDirty: false,
        reset: vi.fn(),
        transform: vi.fn().mockReturnThis(),
    })),
}));

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

vi.mock('../components/AppNav', () => ({
    default: () => <nav data-testid="app-nav" />,
}));

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

const app = {
    id: 'tenant-abc',
    name: 'Kilimo SACCO',
    orgType: 'sacco',
    country: 'TZ',
    subdomain: 'kilimo',
    domain: 'kilimo.upepofinance.com',
    portalUrl: 'https://kilimo.upepofinance.com',
    plan: 'Professional',
    planStatus: 'active',
    billingCycle: 'monthly',
    trialEndsAt: null,
    currentPeriodEnd: null,
    setupComplete: true,
};

describe('App Settings page', () => {
    beforeEach(() => {
        vi.clearAllMocks();
    });

    it('renders inside ClientLayout', () => {
        render(
            <AppSettings app={app as any} domainSuffix=".upepofinance.com" />,
        );
        expect(screen.getByTestId('client-layout')).toBeInTheDocument();
    });

    it('renders AppNav', () => {
        render(
            <AppSettings app={app as any} domainSuffix=".upepofinance.com" />,
        );
        expect(screen.getByTestId('app-nav')).toBeInTheDocument();
    });

    it('renders the "Organisation details" section heading', () => {
        render(
            <AppSettings app={app as any} domainSuffix=".upepofinance.com" />,
        );
        expect(screen.getByText('Organisation details')).toBeInTheDocument();
    });

    it('renders the organisation name input', () => {
        render(
            <AppSettings app={app as any} domainSuffix=".upepofinance.com" />,
        );
        expect(screen.getByText('Organisation name')).toBeInTheDocument();
        expect(screen.getByDisplayValue('Kilimo SACCO')).toBeInTheDocument();
    });

    it('renders country selector', () => {
        render(
            <AppSettings app={app as any} domainSuffix=".upepofinance.com" />,
        );
        expect(screen.getByRole('combobox')).toBeInTheDocument();
    });

    it('renders organisation type buttons', () => {
        render(
            <AppSettings app={app as any} domainSuffix=".upepofinance.com" />,
        );
        expect(
            screen.getByRole('button', { name: /sacco/i }),
        ).toBeInTheDocument();
        expect(
            screen.getByRole('button', { name: /microfinance/i }),
        ).toBeInTheDocument();
    });

    it('renders the Subdomain section with domain suffix', () => {
        render(
            <AppSettings app={app as any} domainSuffix=".upepofinance.com" />,
        );
        expect(screen.getByText('Subdomain')).toBeInTheDocument();
        expect(screen.getByText('.upepofinance.com')).toBeInTheDocument();
    });

    it('renders the portal URL preview', () => {
        render(
            <AppSettings app={app as any} domainSuffix=".upepofinance.com" />,
        );
        expect(screen.getByText('Portal URL preview')).toBeInTheDocument();
        expect(
            screen.getByText('https://kilimo.upepofinance.com'),
        ).toBeInTheDocument();
    });

    it('renders Save settings button', () => {
        render(
            <AppSettings app={app as any} domainSuffix=".upepofinance.com" />,
        );
        expect(
            screen.getByRole('button', { name: /save settings/i }),
        ).toBeInTheDocument();
    });
});
