import { describe, expect, test } from "vitest";

import { parseForm } from "@/shared/forms/validation";
import { humanRoomRobotConfigSchema, robotRoomCreateSchema, roomPinCreateSchema, roomWhitelistSchema } from "./schema";

describe("robot room form schema", () => {
    test("preserves int64 robot user ids as strings", () => {
        const payload = parseForm(robotRoomCreateSchema, {
            candidateRobotUserIds: ["325455441691676672"],
            giftIds: ["84"],
            luckyComboMax: "10000",
            luckyComboMin: "100",
            luckyGiftIds: ["28"],
            luckyPauseMaxMs: "20000",
            luckyPauseMinMs: "5000",
            maxGiftSenders: "2",
            maxRobotCount: "8",
            minRobotCount: "2",
            normalGiftIntervalMs: "10000",
            ownerRobotUserId: "325379237278126080",
            robotReplaceMaxMs: "60000",
            robotReplaceMinMs: "0",
            robotStayMaxMs: "600000",
            robotStayMinMs: "180000",
            seatCount: "10",
            visibleRegionId: 0,
        });

        expect(payload.ownerRobotUserId).toBe("325379237278126080");
        expect(payload.candidateRobotUserIds).toEqual(["325455441691676672"]);
        expect(payload.robotStayMinMs).toBe(180000);
        expect(payload.robotStayMaxMs).toBe(600000);
        expect(payload.robotReplaceMinMs).toBe(0);
        expect(payload.robotReplaceMaxMs).toBe(60000);
        expect(payload.maxGiftSenders).toBe(2);
    });
});

describe("human room robot config schema", () => {
    test("preserves robot ids and parses enabled config", () => {
        const payload = parseForm(humanRoomRobotConfigSchema, {
            candidateRoomMaxOnline: "5",
            countryLimitEnabled: true,
            countryRules: [{ allowedOwnerIds: "1001, 1002,,1001", countryCode: "sa", maxRoomCount: "2" }],
            enabled: true,
            giftIds: ["84"],
            luckyComboMax: "3",
            luckyComboMin: "1",
            luckyGiftIds: ["28"],
            luckyPauseMaxMs: "20000",
            luckyPauseMinMs: "5000",
            maxGiftSenders: "1",
            normalGiftIntervalMaxMs: "20000",
            normalGiftIntervalMinMs: "1000",
            robotReplaceMaxMs: "180000",
            robotReplaceMinMs: "60000",
            robotStayMaxMs: "600000",
            robotStayMinMs: "60000",
            roomTargetMaxOnline: "10",
            roomTargetMinOnline: "8",
            superLuckyGiftIds: ["99"],
        });

        expect(payload.roomTargetMinOnline).toBe(8);
        expect(payload.roomTargetMaxOnline).toBe(10);
        expect(payload.roomFullStopOnline).toBe(10);
        expect(payload.normalGiftIntervalMinMs).toBe(1000);
        expect(payload.normalGiftIntervalMaxMs).toBe(20000);
        expect(payload.giftIds).toEqual(["84"]);
        expect(payload.countryLimitEnabled).toBe(true);
        expect(payload.countryRules).toEqual([{ allowedOwnerIds: ["1001", "1002"], countryCode: "SA", maxRoomCount: 2 }]);
    });

    test("rejects duplicated country rules", () => {
        expect(() =>
            parseForm(humanRoomRobotConfigSchema, {
                candidateRoomMaxOnline: "5",
                countryLimitEnabled: true,
                countryRules: [
                    { countryCode: "SA", maxRoomCount: "2" },
                    { countryCode: "sa", maxRoomCount: "3" },
                ],
                enabled: true,
                giftIds: ["84"],
                luckyComboMax: "3",
                luckyComboMin: "1",
                luckyGiftIds: ["28"],
                luckyPauseMaxMs: "20000",
                luckyPauseMinMs: "5000",
                maxGiftSenders: "1",
                normalGiftIntervalMaxMs: "20000",
                normalGiftIntervalMinMs: "1000",
                robotReplaceMaxMs: "180000",
                robotReplaceMinMs: "60000",
                robotStayMaxMs: "600000",
                robotStayMinMs: "60000",
                roomTargetMaxOnline: "10",
                roomTargetMinOnline: "8",
                superLuckyGiftIds: [],
            }),
        ).toThrow("国家不能重复");
    });

    test("requires gift pools when enabled", () => {
        expect(() =>
            parseForm(humanRoomRobotConfigSchema, {
                candidateRoomMaxOnline: "5",
                enabled: true,
                giftIds: [],
                luckyComboMax: "3",
                luckyComboMin: "1",
                luckyGiftIds: [],
                luckyPauseMaxMs: "20000",
                luckyPauseMinMs: "5000",
                maxGiftSenders: "1",
                normalGiftIntervalMaxMs: "20000",
                normalGiftIntervalMinMs: "1000",
                robotReplaceMaxMs: "180000",
                robotReplaceMinMs: "60000",
                robotStayMaxMs: "600000",
                robotStayMinMs: "60000",
                roomTargetMaxOnline: "10",
                roomTargetMinOnline: "8",
                superLuckyGiftIds: [],
            }),
        ).toThrow("启用后请选择普通礼物合集");
    });
});

describe("room whitelist schema", () => {
    test("keeps long user ids as strings and removes duplicates", () => {
        const payload = parseForm(roomWhitelistSchema, {
            userIds: ["325379237278126080", "325379237278126080", "1001"],
        });

        expect(payload.userIds).toEqual(["1001", "325379237278126080"]);
    });
});

describe("room pin create schema", () => {
    test("accepts country pin type", () => {
        const payload = parseForm(roomPinCreateSchema, {
            expiresAtMs: "2026-07-23T19:17",
            pinType: "country",
            pinnedAtMs: "2026-06-23T19:17",
            roomId: "lalu_20d8aba4-23da-44d2-aa6e-876f88",
            weight: "0",
        });

        expect(payload.pinType).toBe("country");
        expect(payload.roomId).toBe("lalu_20d8aba4-23da-44d2-aa6e-876f88");
        expect(payload.weight).toBe(0);
    });
});
