import { z } from "zod";

export const CastSchema = z.object({
	adult: z.boolean(),
	gender: z.number(),
	id: z.number(),
	knownForDepartment: z.string(),
	name: z.string(),
	originalName: z.string(),
	popularity: z.number(),
	profilePath: z.string().nullable(),
	castId: z.number(),
	character: z.string(),
	creditId: z.string(),
	order: z.number(),
});

export type Cast = z.infer<typeof CastSchema>;

export const CrewSchema = z.object({
	adult: z.boolean(),
	gender: z.number(),
	id: z.number(),
	knownForDepartment: z.string(),
	name: z.string(),
	originalName: z.string(),
	popularity: z.number(),
	profilePath: z.string().nullable(),
	creditId: z.string(),
	department: z.string(),
	job: z.string(),
});

export type Crew = z.infer<typeof CrewSchema>;

// Info for fetching credits by title
export const TitleCreditsInfoSchema = z.discriminatedUnion("titleType", [
	z.object({
		titleType: z.literal("MOVIE"),
		providerId: z.string(),
	}),
	z.discriminatedUnion("area", [
		z.object({
			titleType: z.literal("SERIES"),
			area: z.literal("SERIES"),
			providerId: z.string(),
		}),
		z.object({
			titleType: z.literal("SERIES"),
			area: z.literal("SEASON"),
			providerId: z.string(),
			seasonNumber: z.number(),
		}),
		z.object({
			titleType: z.literal("SERIES"),
			area: z.literal("EPISODE"),
			providerId: z.string(),
			seasonNumber: z.number(),
			episodeNumber: z.number(),
		}),
	]),
]);

export type TitleCreditsInfo = z.infer<typeof TitleCreditsInfoSchema>;

// Resolved credits for a title (cast + crew)
export const ResolvedTitleCreditsSchema = z.object({
	cast: z.array(CastSchema),
	crew: z.array(CrewSchema),
});

export type ResolvedTitleCredits = z.infer<typeof ResolvedTitleCreditsSchema>;
export type ResolvedTitleCreditsResult = ResolvedTitleCredits | null;
