import { z } from "zod";

export const createItemSchema = z.object({
	name: z.string().min(1, "Name is required"),
	weightGrams: z.number().nonnegative().optional(),
	priceCents: z.number().int().nonnegative().optional(),
	categoryId: z.number().int().positive(),
	notes: z.string().optional(),
	productUrl: z.string().url().optional().or(z.literal("")),
	imageFilename: z.string().optional(),
	imageSourceUrl: z.string().url().optional().or(z.literal("")),
	quantity: z.number().int().positive().optional(),
	globalItemId: z.number().int().positive().optional(),
	purchasePriceCents: z.number().int().nonnegative().optional(),
	brand: z.string().optional(),
	dominantColor: z.string().nullable().optional(),
	cropZoom: z.number().nullable().optional(),
	cropX: z.number().nullable().optional(),
	cropY: z.number().nullable().optional(),
});

export const updateItemSchema = createItemSchema.partial().extend({
	id: z.number().int().positive(),
});

export const createCategorySchema = z.object({
	name: z.string().min(1, "Category name is required"),
	icon: z.string().min(1).max(50).default("package"),
});

export const updateCategorySchema = z.object({
	id: z.number().int().positive(),
	name: z.string().min(1).optional(),
	icon: z.string().min(1).max(50).optional(),
});

// Thread schemas
export const createThreadSchema = z.object({
	name: z.string().min(1, "Thread name is required"),
	categoryId: z.number().int().positive(),
});

export const updateThreadSchema = z.object({
	name: z.string().min(1).optional(),
	categoryId: z.number().int().positive().optional(),
});

// Candidate status
export const candidateStatusSchema = z.enum([
	"researching",
	"ordered",
	"arrived",
]);

// Candidate schemas (same fields as items)
export const createCandidateSchema = z.object({
	name: z.string().min(1, "Name is required"),
	weightGrams: z.number().nonnegative().optional(),
	priceCents: z.number().int().nonnegative().optional(),
	categoryId: z.number().int().positive(),
	notes: z.string().optional(),
	productUrl: z.string().url().optional().or(z.literal("")),
	imageFilename: z.string().optional(),
	imageSourceUrl: z.string().url().optional().or(z.literal("")),
	status: candidateStatusSchema.optional(),
	pros: z.string().optional(),
	cons: z.string().optional(),
	globalItemId: z.number().int().positive().optional(),
	dominantColor: z.string().nullable().optional(),
	cropZoom: z.number().nullable().optional(),
	cropX: z.number().nullable().optional(),
	cropY: z.number().nullable().optional(),
});

export const updateCandidateSchema = createCandidateSchema.partial();

export const resolveThreadSchema = z.object({
	candidateId: z.number().int().positive(),
});

export const reorderCandidatesSchema = z.object({
	orderedIds: z.array(z.number().int().positive()).min(1),
});

// Setup schemas
export const createSetupSchema = z.object({
	name: z.string().min(1, "Setup name is required"),
	visibility: z
		.enum(["private", "link", "public"])
		.optional()
		.default("private"),
});

export const updateSetupSchema = z.object({
	name: z.string().min(1, "Setup name is required"),
	visibility: z.enum(["private", "link", "public"]).optional(),
});

export const syncSetupItemsSchema = z.object({
	itemIds: z.array(z.number().int().positive()),
});

// Classification schemas
export const classificationSchema = z.enum(["base", "worn", "consumable"]);

export const updateClassificationSchema = z.object({
	classification: classificationSchema,
});

// Global item schemas
export const searchGlobalItemsSchema = z.object({
	q: z.string().optional(),
	tags: z.string().optional(),
});

// Catalog upsert schemas
export const upsertGlobalItemSchema = z.object({
	brand: z.string().min(1, "Brand is required"),
	model: z.string().min(1, "Model is required"),
	category: z.string().optional(),
	weightGrams: z.number().nonnegative().optional(),
	priceCents: z.number().int().nonnegative().optional(),
	imageUrl: z.string().url().optional().or(z.literal("")),
	description: z.string().optional(),
	sourceUrl: z.string().url().optional().or(z.literal("")),
	imageCredit: z.string().optional(),
	imageSourceUrl: z.string().url().optional().or(z.literal("")),
	tags: z.array(z.string().min(1).max(100)).max(20).optional(),
	dominantColor: z.string().nullable().optional(),
	cropZoom: z.number().nullable().optional(),
	cropX: z.number().nullable().optional(),
	cropY: z.number().nullable().optional(),
});

export const bulkUpsertGlobalItemsSchema = z.object({
	items: z.array(upsertGlobalItemSchema).min(1).max(100),
});

// Profile schemas
export const updateProfileSchema = z.object({
	displayName: z.string().max(100).optional(),
	avatarUrl: z.string().nullable().optional(),
	bio: z.string().max(500).optional(),
});

// Account management schemas (per D-05)
export const changePasswordSchema = z.object({
	currentPassword: z.string().min(1),
	newPassword: z.string().min(8),
});

export const changeEmailSchema = z.object({
	newEmail: z.string().email(),
});

export const deleteAccountSchema = z.object({
	confirmation: z.literal("DELETE"),
});

// Onboarding schemas
export const completeOnboardingSchema = z.object({
	globalItemIds: z.array(z.number().int().positive()).max(50),
});
