import { render, screen } from "@testing-library/react";
import { afterEach, expect, test, vi } from "vitest";
import { AppUserPrettyIdsPage } from "./AppUserPrettyIdsPage.jsx";
import { usePrettyIdsPage } from "@/features/app-users/hooks/usePrettyIdsPage.js";

vi.mock("@/features/app-users/hooks/usePrettyIdsPage.js", () => ({
    usePrettyIdsPage: vi.fn(),
}));

afterEach(() => {
    vi.clearAllMocks();
});

test("pretty id page uses tabs and hides internal pretty record ids", () => {
    vi.mocked(usePrettyIdsPage).mockReturnValue(pageFixture());

    render(<AppUserPrettyIdsPage />);

    expect(screen.getByRole("tab", { name: "靓号列表 1" })).toHaveAttribute("aria-selected", "true");
    expect(screen.getByRole("tab", { name: "靓号池 1" })).toBeInTheDocument();
    expect(screen.getAllByText("777777").length).toBeGreaterThan(0);
    expect(screen.getByText("10001")).toBeInTheDocument();
    expect(screen.queryByText("pretty_1782492525506_0b70bb6c6dbc63cf")).not.toBeInTheDocument();
    expect(screen.getByText("占用人 A")).toBeInTheDocument();
    expect(document.querySelector('img[src="https://cdn.example/owner-a.png"]')).toBeInTheDocument();
    expect(screen.getByText("Admin ID 8")).toBeInTheDocument();
    expect(screen.getByText("释放 2026-06-27 01:00:00")).toBeInTheDocument();
    expect(screen.getAllByLabelText("回收").length).toBeGreaterThan(0);
});

test("pretty id page can switch to pool tab", () => {
    const page = pageFixture({ activeTab: "pools" });
    vi.mocked(usePrettyIdsPage).mockReturnValue(page);

    render(<AppUserPrettyIdsPage />);

    expect(screen.getByRole("tab", { name: "靓号池 1" })).toHaveAttribute("aria-selected", "true");
    expect(screen.getByText("≥LV.10")).toBeInTheDocument();
});

function pageFixture(patch = {}) {
    return {
        abilities: {
            canPrettyGenerate: true,
            canPrettyGrant: true,
            canPrettyUpdate: true,
        },
        activeDialog: "",
        activePool: null,
        activePrettyID: null,
        activeTab: "ids",
        assignedUserId: "",
        changeAssignedUserId: vi.fn(),
        changeIDKeyword: vi.fn(),
        changeIDPool: vi.fn(),
        changeIDSource: vi.fn(),
        changeIDStatus: vi.fn(),
        changePoolStatus: vi.fn(),
        changePoolTrack: vi.fn(),
        closeDialog: vi.fn(),
        generateForm: { count: 100, poolId: "", ruleConfigJson: "", ruleType: "aabbcc" },
        grantForm: { displayUserId: "", durationDays: "", reason: "", targetUserId: "" },
        idKeyword: "",
        idPage: 1,
        idPoolId: "",
        idQuery: {
            data: {
                items: [prettyIdFixture()],
                page: 1,
                pageSize: 20,
                total: 1,
            },
            error: null,
            loading: false,
            reload: vi.fn(),
        },
        idSource: "",
        idStatus: "",
        loadingAction: "",
        openCreatePool: vi.fn(),
        openEditPool: vi.fn(),
        openGenerate: vi.fn(),
        openGrant: vi.fn(),
        openStatus: vi.fn(),
        poolForm: {
            levelTrack: "wealth",
            maxLevel: 19,
            minLevel: 10,
            name: "",
            ruleConfigJson: "",
            ruleType: "aabbcc",
            sortOrder: 0,
            status: "active",
        },
        poolOptions: [{ label: "≥LV.10 · 10-19", value: "pool-1" }],
        poolPage: 1,
        poolQuery: {
            data: {
                items: [poolFixture()],
                page: 1,
                pageSize: 20,
                total: 1,
            },
            error: null,
            loading: false,
            reload: vi.fn(),
        },
        poolStatus: "",
        poolTrack: "",
        reloadAll: vi.fn(),
        resetIDFilters: vi.fn(),
        resetPoolFilters: vi.fn(),
        recyclePrettyID: vi.fn(),
        setActiveTab: vi.fn(),
        setGenerateForm: vi.fn(),
        setGrantForm: vi.fn(),
        setIdPage: vi.fn(),
        setPoolForm: vi.fn(),
        setPoolPage: vi.fn(),
        setStatusForm: vi.fn(),
        statusForm: { prettyId: "", reason: "", status: "available" },
        submitGenerate: vi.fn(),
        submitGrant: vi.fn(),
        submitPool: vi.fn(),
        submitStatus: vi.fn(),
        ...patch,
    };
}

function poolFixture() {
    return {
        createdAtMs: 1781249886288,
        levelTrack: "wealth",
        maxLevel: 19,
        minLevel: 10,
        name: "≥LV.10",
        poolId: "pretty_pool_1781249886288_301d1a449c51fbc0",
        ruleType: "abccba",
        status: "active",
        updatedAtMs: 1781249886288,
    };
}

function prettyIdFixture() {
    return {
        assignedAtMs: 1782499200000,
        assignedUser: {
            avatar: "https://cdn.example/owner-a.png",
            defaultDisplayUserId: "10001",
            displayUserId: "777777",
            userId: "9001",
            username: "占用人 A",
        },
        assignedUserId: "9001",
        createdAtMs: 1782499100000,
        createdByAdminId: "7",
        displayUserId: "777777",
        operator: {
            account: "ops",
            adminId: "8",
            name: "运营",
            type: "admin",
        },
        pool: poolFixture(),
        poolId: "pool-1",
        prettyId: "pretty_1782492525506_0b70bb6c6dbc63cf",
        releasedAtMs: 1782493200000,
        source: "admin_grant",
        status: "assigned",
        updatedAtMs: 1782499300000,
        updatedByAdminId: "8",
    };
}
