import type { Page } from "@playwright/test";

/** Matches mock-data DD Admin row used on LoginPage quick-login cards */
export const E2E_DD_ADMIN = {
    id: "18",
    fullName: "Lince",
    email: "lince@royalenfield.com",
    password: "Admin@123",
    role: "DD Admin",
} as const;

export type StubApplication = Record<string, unknown>;

const defaultApplications: StubApplication[] = [
    {
        id: "e2e-visible-1",
        applicationId: "E2E-REG-001",
        applicantName: "E2E Visible Dealer",
        email: "dealer.e2e@example.com",
        phone: "+91 9000000001",
        overallStatus: "FDD",
        ddLeadShortlisted: true,
        createdAt: new Date().toISOString(),
    },
];

/** Opportunity Requests: not yet DD Lead–shortlisted + early-stage status */
export const opportunityRequestsApplications: StubApplication[] = [
    {
        id: "e2e-opp-1",
        applicationId: "E2E-OPP-001",
        applicantName: "E2E Opportunity Dealer",
        email: "opp.e2e@example.com",
        phone: "+91 9000000002",
        overallStatus: "Questionnaire Pending",
        ddLeadShortlisted: false,
        preferredLocation: "Mumbai",
        state: "Maharashtra",
        createdAt: new Date().toISOString(),
    },
];

/**
 * Stubs REST calls so E2E runs without a live API. Matches default VITE_API_URL
 * host (localhost:5000) and path prefix /api.
 */
export async function installOnboardingApiStubs(
    page: Page,
    options?: { applications?: StubApplication[] }
): Promise<void> {
    const apps = options?.applications ?? defaultApplications;

    await page.route("**/api/auth/login", async (route) => {
        if (route.request().method() !== "POST") {
            await route.continue();
            return;
        }
        await route.fulfill({
            status: 200,
            contentType: "application/json",
            body: JSON.stringify({
                token: "e2e-playwright-token",
                user: {
                    id: E2E_DD_ADMIN.id,
                    fullName: E2E_DD_ADMIN.fullName,
                    email: E2E_DD_ADMIN.email,
                    role: E2E_DD_ADMIN.role,
                },
            }),
        });
    });

    await page.route("**/api/auth/me", async (route) => {
        if (route.request().method() !== "GET") {
            await route.continue();
            return;
        }
        await route.fulfill({
            status: 200,
            contentType: "application/json",
            body: JSON.stringify({
                user: {
                    id: E2E_DD_ADMIN.id,
                    fullName: E2E_DD_ADMIN.fullName,
                    email: E2E_DD_ADMIN.email,
                    role: E2E_DD_ADMIN.role,
                    roleCode: E2E_DD_ADMIN.role,
                },
            }),
        });
    });

    await page.route("**/api/onboarding/applications", async (route) => {
        if (route.request().method() !== "GET") {
            await route.continue();
            return;
        }
        await route.fulfill({
            status: 200,
            contentType: "application/json",
            body: JSON.stringify({ data: apps }),
        });
    });

    await page.route("**/api/admin/users", async (route) => {
        if (route.request().method() !== "GET") {
            await route.continue();
            return;
        }
        await route.fulfill({
            status: 200,
            contentType: "application/json",
            body: JSON.stringify({
                success: true,
                data: [
                    {
                        id: "assignee-1",
                        fullName: "E2E Assignee",
                        email: "assignee.e2e@example.com",
                    },
                ],
            }),
        });
    });
}
