import { expect, test } from "vitest";
import {
    agencyCloseSchema,
    agencyJoinEnabledSchema,
    coinSellerStatusSchema,
    coinSellerSubApplicationReviewSchema,
    createCoinSellerSchema,
    createManagerSchema,
    countryRenameCodeSchema,
    countryUpdateSchema,
} from "./schema";

test("coin seller contact accepts plus sign with large internal user id", () => {
    const result = coinSellerStatusSchema.safeParse({
        commandId: "coin-seller-edit-test",
        contact: "+63",
        reason: "update contact",
        status: "active",
        targetUserId: "320756743338463232",
    });

    expect(result.success).toBe(true);
    if (result.success) {
        expect(result.data.contact).toBe("+63");
        expect(result.data.targetUserId).toBe("320756743338463232");
    }
});

test("coin seller schemas preserve sub seller permission defaults and explicit false", () => {
    const createDefault = createCoinSellerSchema.safeParse({
        commandId: "coin-seller-create-test",
        targetUserId: "1003",
    });
    const statusDefault = coinSellerStatusSchema.safeParse({
        commandId: "coin-seller-edit-test",
        status: "active",
        targetUserId: "320756743338463232",
    });
    const statusDisabled = coinSellerStatusSchema.safeParse({
        canManageSubCoinSellers: false,
        commandId: "coin-seller-edit-test",
        status: "active",
        targetUserId: "320756743338463232",
    });

    expect(createDefault.success).toBe(true);
    expect(statusDefault.success).toBe(true);
    expect(statusDisabled.success).toBe(true);
    if (createDefault.success) {
        expect(createDefault.data.canManageSubCoinSellers).toBe(true);
    }
    if (statusDefault.success) {
        expect(statusDefault.data).not.toHaveProperty("canManageSubCoinSellers");
    }
    if (statusDisabled.success) {
        expect(statusDisabled.data.canManageSubCoinSellers).toBe(false);
    }
});

test("coin seller sub application review schema keeps command id and reason", () => {
    const result = coinSellerSubApplicationReviewSchema.safeParse({
        commandId: "coin-seller-sub-application-review-test",
        reason: "same region and profile is active",
    });

    expect(result.success).toBe(true);
    if (result.success) {
        expect(result.data.commandId).toBe("coin-seller-sub-application-review-test");
        expect(result.data.reason).toBe("same region and profile is active");
    }
});

test("agency action schemas preserve large agency ids as strings", () => {
    const agencyId = "321170072154411009";
    const closeResult = agencyCloseSchema.safeParse({
        agencyId,
        commandId: "agency-close-test",
        reason: "close agency",
    });
    const joinResult = agencyJoinEnabledSchema.safeParse({
        agencyId,
        commandId: "agency-join-test",
        joinEnabled: false,
        reason: "disable join",
    });

    expect(closeResult.success).toBe(true);
    expect(joinResult.success).toBe(true);
    if (closeResult.success) {
        expect(closeResult.data.agencyId).toBe(agencyId);
    }
    if (joinResult.success) {
        expect(joinResult.data.agencyId).toBe(agencyId);
    }
});

test("country update schema excludes country code from metadata update payload", () => {
    const result = countryUpdateSchema.safeParse({
        countryCode: "phl",
        countryDisplayName: "Philippines Display",
        countryName: "Philippines",
        sortOrder: "1770",
    });

    expect(result.success).toBe(true);
    if (result.success) {
        expect(result.data).not.toHaveProperty("countryCode");
        expect(result.data).not.toHaveProperty("enabled");
    }
});

test("country rename schema keeps editable country code", () => {
    const result = countryRenameCodeSchema.safeParse({
        countryCode: "phl",
        countryDisplayName: "Philippines Display",
        countryName: "Philippines",
        sortOrder: "1770",
    });

    expect(result.success).toBe(true);
    if (result.success) {
        expect(result.data.countryCode).toBe("PHL");
        expect(result.data).not.toHaveProperty("enabled");
    }
});

test("manager schema defaults vip grant permission on and preserves explicit false", () => {
    const defaultResult = createManagerSchema.safeParse({ targetUserId: "1003" });
    const disabledResult = createManagerSchema.safeParse({ canGrantVip: false, targetUserId: "1003" });

    expect(defaultResult.success).toBe(true);
    expect(disabledResult.success).toBe(true);
    if (defaultResult.success) {
        expect(defaultResult.data.canGrantVip).toBe(true);
        expect(defaultResult.data.canManageCrossRegion).toBe(false);
    }
    if (disabledResult.success) {
        expect(disabledResult.data.canGrantVip).toBe(false);
    }
});
