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

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

jest.mock("@/api/API", () => ({
    API: {
        getDealerDashboard: jest.fn(),
        getConstitutionalChanges: jest.fn(),
        getConstitutionalChangeMeta: jest.fn(),
        createConstitutionalChange: jest.fn(),
    },
}))

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

    beforeEach(() => {
        jest.clearAllMocks()
        jest.mocked(API.getDealerDashboard).mockResolvedValue({
            data: {
                profile: {
                    constitutionType: "Proprietorship",
                    dealerCode: "DLR-99",
                    businessName: "Test Motors",
                },
            },
        } as never)
        jest.mocked(API.getConstitutionalChanges).mockResolvedValue({
            data: { requests: [] },
        } as never)
        jest.mocked(API.getConstitutionalChangeMeta).mockResolvedValue({
            data: {
                success: true,
                structureTargets: [
                    { value: "Partnership", label: "Partnership" },
                    { value: "LLP", label: "LLP" },
                ],
            },
        } as never)
    })

    function setup() {
        render(
            <DealerConstitutionalChangePage onViewDetails={onViewDetails} />,
        )
    }

    it("renders dealer heading and stats after loading", async () => {
        setup()

        expect(
            await screen.findByRole("heading", {
                level: 1,
                name: /my constitutional change requests/i,
            }),
        ).toBeInTheDocument()

        expect(API.getDealerDashboard).toHaveBeenCalled()

        expect(screen.getByText("Total Requests")).toBeInTheDocument()
        expect(screen.getByText("Pending")).toBeInTheDocument()
        expect(screen.getByText("Completed")).toBeInTheDocument()
    })

    it("shows empty table message when no requests", async () => {
        setup()

        await waitFor(() => {
            expect(
                screen.getByText(
                    /no constitutional change requests found/i,
                ),
            ).toBeInTheDocument()
        })
    })

    it("opens submit dialog from New Constitutional Change", async () => {
        const user = userEvent.setup()
        setup()

        await screen.findByRole("heading", {
            level: 1,
            name: /my constitutional change requests/i,
        })

        await user.click(
            await screen.findByRole("button", {
                name: /new constitutional change/i,
            }),
        )

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

        expect(screen.getByText(/current dealership information/i)).toBeInTheDocument()
    })
})
