import { screen } from "@testing-library/react"
import userEvent from "@testing-library/user-event"
import { NonOpportunitiesPage } from "../pages/NonOpportunitiesPage"
import { renderWithProviders } from "./test-utils"
import { SHARED_APP_STATUSES } from "@/testing/constants"
import { mockOnboardingService, createGlobalMockApplication } from "@/testing/mocks"

describe("NonOpportunitiesPage - Lead generation filtering", () => {
    beforeEach(() => {
        jest.clearAllMocks()
    })

    const renderPage = (apps: any[] = []) => {
        ;(mockOnboardingService.getApplications as jest.Mock).mockResolvedValue({
            success: true,
            data: apps,
        })
        return renderWithProviders(
            <NonOpportunitiesPage onViewDetails={jest.fn()} />,
            {
                preloadedState: {
                    auth: {
                        user: { id: "admin-1", role: "DD Lead" } as any,
                        token: "tok",
                        isAuthenticated: true,
                        loading: false,
                        error: null,
                    },
                },
            }
        )
    }

    it("should only list Submitted leads as non-opportunities", async () => {
        const mockApps = [
            createGlobalMockApplication({
                id: "lead-1",
                applicantName: "Lead Submitted Only",
                overallStatus: SHARED_APP_STATUSES.SUBMITTED,
                preferredLocation: "Mumbai",
                state: "Maharashtra",
            }),
            createGlobalMockApplication({
                id: "lead-2",
                applicantName: "Not A Lead Row",
                overallStatus: SHARED_APP_STATUSES.LOI,
                preferredLocation: "Pune",
                state: "Maharashtra",
            }),
        ]
        renderPage(mockApps)

        expect(await screen.findByText("Lead Submitted Only")).toBeInTheDocument()
        expect(screen.queryByText("Not A Lead Row")).not.toBeInTheDocument()
        expect(screen.getByTestId("onboarding-non-opps-stat-total")).toHaveTextContent(
            "Total Leads"
        )
        expect(screen.getByTestId("onboarding-non-opps-stat-total")).toHaveTextContent("1")
    })

    it("should narrow results when searching", async () => {
        const user = userEvent.setup()
        const mockApps = [
            createGlobalMockApplication({
                id: "s1",
                applicantName: "Alpha Lead",
                overallStatus: SHARED_APP_STATUSES.SUBMITTED,
            }),
            createGlobalMockApplication({
                id: "s2",
                applicantName: "Beta Lead",
                overallStatus: SHARED_APP_STATUSES.SUBMITTED,
            }),
        ]
        renderPage(mockApps)

        expect(await screen.findByText("Alpha Lead")).toBeInTheDocument()

        const searchInput = screen.getByTestId("onboarding-non-opps-search-input")
        await user.type(searchInput, "Beta")

        expect(screen.getByText("Beta Lead")).toBeInTheDocument()
        expect(screen.queryByText("Alpha Lead")).not.toBeInTheDocument()
    })
})
