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

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

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

const dealerUser = {
    id: "d1",
    name: "Dealer",
    email: "d@test.com",
    password: "",
    role: "Dealer" as const,
}

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

    beforeEach(() => {
        jest.clearAllMocks()
        jest.mocked(API.getDealerDashboard).mockResolvedValue({
            data: {
                outlets: [
                    {
                        id: "out-1",
                        name: "City Studio",
                        code: "CS-1",
                        status: "Active",
                        location: "MG Road, Bangalore",
                        type: "Dealership",
                    },
                ],
                profile: { businessName: "City Motors" },
            },
        } as never)
        jest.mocked(API.getResignations).mockResolvedValue({
            data: { success: true, resignations: [] },
        } as never)
    })

    it("renders management heading and stats after loading", async () => {
        render(
            <DealerResignationPage
                currentUser={dealerUser}
                onViewDetails={onViewDetails}
            />,
        )

        expect(
            await screen.findByRole("heading", {
                level: 1,
                name: /dealership resignation management/i,
            }),
        ).toBeInTheDocument()

        expect(screen.getByText("Total Outlets")).toBeInTheDocument()
        expect(API.getDealerDashboard).toHaveBeenCalled()
        expect(API.getResignations).toHaveBeenCalled()
    })

    it("shows empty requests table", async () => {
        render(
            <DealerResignationPage
                currentUser={dealerUser}
                onViewDetails={onViewDetails}
            />,
        )

        expect(
            await screen.findByText(/no resignation requests found/i),
        ).toBeInTheDocument()
    })

    it("opens submit resignation dialog from outlet card", async () => {
        const user = userEvent.setup()
        render(
            <DealerResignationPage
                currentUser={dealerUser}
                onViewDetails={onViewDetails}
            />,
        )

        await screen.findByRole("heading", {
            level: 1,
            name: /dealership resignation management/i,
        })

        await user.click(
            screen.getByRole("button", { name: /request resignation/i }),
        )

        expect(
            screen.getByRole("heading", {
                name: /submit resignation request/i,
            }),
        ).toBeInTheDocument()
    })

    it("calls onViewDetails with resignationId from View", async () => {
        const user = userEvent.setup()
        jest.mocked(API.getResignations).mockResolvedValue({
            data: {
                success: true,
                resignations: [
                    {
                        id: "uuid-1",
                        resignationId: "RSG-2025-42",
                        status: "Pending Review",
                        resignationType: "Voluntary",
                        submittedOn: "2025-07-01T08:00:00.000Z",
                        outlet: { name: "City Studio" },
                        progressPercentage: 35,
                    },
                ],
            },
        } as never)

        render(
            <DealerResignationPage
                currentUser={dealerUser}
                onViewDetails={onViewDetails}
            />,
        )

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

        expect(onViewDetails).toHaveBeenCalledWith("RSG-2025-42")
    })
})
