import { z } from "zod";
import { assertType } from "~/utils/types";
import {
	ability,
	hexCodeWithoutAlpha,
	modeShort,
	specialWeaponId,
	stageId,
	subWeaponId,
	weaponSplId,
} from "~/utils/zod";

export const tierListItemTypeSchema = z.enum([
	"main-weapon",
	"sub-weapon",
	"special-weapon",
	"stage",
	"mode",
	"stage-mode",
	"ability",
]);
assertType<z.infer<typeof tierListItemTypeSchema>, TierListItem["type"]>();

const tierListItemSchema = z.union([
	z.object({
		id: weaponSplId,
		nth: z.number().optional(),
		type: z.literal("main-weapon"),
	}),
	z.object({
		id: subWeaponId,
		nth: z.number().optional(),
		type: z.literal("sub-weapon"),
	}),
	z.object({
		id: specialWeaponId,
		nth: z.number().optional(),
		type: z.literal("special-weapon"),
	}),
	z.object({
		id: stageId,
		nth: z.number().optional(),
		type: z.literal("stage"),
	}),
	z.object({
		id: modeShort,
		nth: z.number().optional(),
		type: z.literal("mode"),
	}),
	z.object({
		id: z.string(),
		nth: z.number().optional(),
		type: z.literal("stage-mode"),
	}),
	z.object({
		id: ability,
		nth: z.number().optional(),
		type: z.literal("ability"),
	}),
]);

export type TierListItem = z.infer<typeof tierListItemSchema>;

const tierSchema = z.object({
	id: z.string(),
	name: z.string(),
	color: hexCodeWithoutAlpha,
});

export type TierListMakerTier = z.infer<typeof tierSchema>;

type TierListItemSchemaType = z.infer<typeof tierListItemSchema>;

export const tierListStateSerializedSchema = z.object({
	tiers: z.array(tierSchema),
	tierItems: z.array(z.tuple([z.string(), z.array(tierListItemSchema)])),
});

export type TierListState = {
	tiers: Array<TierListMakerTier>;
	tierItems: Map<string, TierListItemSchemaType[]>;
};
