import { render, screen } from "@testing-library/react"
import userEvent from "@testing-library/user-event"
import { API } from "@/api/API"
import { ResignationPage } from "../pages/ResignationPage"

jest.mock("sonner", () => ({
    toast: {
        error: jest.fn(),
        success: jest.fn(),
    },
}))

jest.mock("@/api/API", () => ({
    API: {
        getResignations: jest.fn(),
    },
}))

const superAdmin = {
    id: "adm",
    name: "Super",
    email: "s@test.com",
    password: "",
    role: "Super Admin" as const,
}

describe("ResignationPage", () => {
    const onViewDetails = jest.fn()

    beforeEach(() => {
        jest.clearAllMocks()
        jest.mocked(API.getResignations).mockResolvedValue({
            data: { success: true, resignations: [] },
        } as never)
    })

    it("renders stats and empty list after loading", async () => {
        render(
            <ResignationPage
                currentUser={superAdmin}
                onViewDetails={onViewDetails}
            />,
        )

        expect(
            screen.getByRole("heading", { name: /resignation requests/i }),
        ).toBeInTheDocument()
        expect(screen.getByText("Requires Your Action")).toBeInTheDocument()
        expect(
            await screen.findByText(/no resignation requests found/i),
        ).toBeInTheDocument()
        expect(API.getResignations).toHaveBeenCalled()
    })

    it("calls onViewDetails from View Details", async () => {
        const user = userEvent.setup()
        jest.mocked(API.getResignations).mockResolvedValue({
            data: {
                success: true,
                resignations: [
                    {
                        id: "res-internal-id",
                        resignationId: "RSG-100",
                        status: "Pending ASM Review",
                        currentStage: "ASM Review",
                        resignationType: "Voluntary",
                        reason: "Business reasons",
                        submittedOn: "2025-06-01T10:00:00.000Z",
                        outlet: {
                            name: "South Hub",
                            code: "SH-01",
                            city: "Coimbatore",
                            state: "TN",
                        },
                    },
                ],
            },
        } as never)

        render(
            <ResignationPage
                currentUser={superAdmin}
                onViewDetails={onViewDetails}
            />,
        )

        await user.click(
            await screen.findByRole("button", { name: /view details/i }),
        )

        expect(onViewDetails).toHaveBeenCalledWith("res-internal-id")
    })
})
