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

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: {},
        },
    })),
}));

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

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

vi.mock('@/components/admin/PlanBadge', () => ({
    default: ({ plan }: any) => <span data-testid="plan-badge">{plan}</span>,
}));

vi.mock('@/components/admin/TenantStatusBadge', () => ({
    default: ({ status }: any) => (
        <span data-testid="status-badge">{status}</span>
    ),
}));

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

const CLIENT = {
    id: 'user-1',
    name: 'Wanjiku Kamau',
    email: 'wanjiku@example.com',
    avatar: null,
    provider: 'email',
    email_verified_at: '2025-01-15T00:00:00Z',
    joined_at: '2025-01-10T00:00:00Z',
};

const APPS = [
    {
        id: 'umoja-sacco',
        name: 'Umoja Sacco',
        plan: 'Starter',
        status: 'trial',
        is_owner: true,
        membership_status: 'active',
        joined_at: '2025-01-15T00:00:00Z',
    },
];

const DEFAULT_PROPS: React.ComponentProps<typeof ClientsShow> = {
    client: CLIENT,
    apps: APPS,
    stats: { owned_apps: 1, total_mrr: 'USD 49' },
    activity: [],
};

function renderShow(overrides: Partial<typeof DEFAULT_PROPS> = {}) {
    return render(<ClientsShow {...DEFAULT_PROPS} {...overrides} />);
}

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

describe('Admin/Clients/Show', () => {
    it('renders inside AdminLayout without crashing', () => {
        renderShow();
        expect(screen.getByTestId('admin-layout')).toBeInTheDocument();
    });

    it('displays the client name and email', () => {
        renderShow();
        expect(
            screen.getByRole('heading', { name: 'Wanjiku Kamau' }),
        ).toBeInTheDocument();
        expect(
            screen.getAllByText('wanjiku@example.com').length,
        ).toBeGreaterThan(0);
    });

    it('shows a verified badge for verified clients', () => {
        renderShow();
        expect(screen.getByText('verified')).toBeInTheDocument();
    });

    it('shows resend verification button only for unverified clients', () => {
        renderShow();
        expect(
            screen.queryByRole('button', { name: /resend verification/i }),
        ).not.toBeInTheDocument();

        renderShow({
            client: { ...CLIENT, email_verified_at: null },
        });
        expect(
            screen.getByRole('button', { name: /resend verification/i }),
        ).toBeInTheDocument();
    });

    it('lists the client tenants with links', () => {
        renderShow();
        expect(screen.getByText('Umoja Sacco')).toBeInTheDocument();
        const link = screen
            .getAllByRole('link')
            .find(
                (l) => l.getAttribute('href') === '/admin/tenants/umoja-sacco',
            );
        expect(link).toBeDefined();
    });

    it('shows the combined MRR stat', () => {
        renderShow();
        expect(screen.getByText('USD 49')).toBeInTheDocument();
    });

    it('shows an empty state when the client has no tenants', () => {
        renderShow({ apps: [] });
        expect(
            screen.getByText(/hasn't created or joined any/i),
        ).toBeInTheDocument();
    });

    it('renders a back link to /admin/clients', () => {
        renderShow();
        expect(
            screen.getByRole('link', { name: /all clients/i }),
        ).toHaveAttribute('href', '/admin/clients');
    });
});
