import { z } from "astro/zod";

import type { FieldDefinition, FieldUIHints, FileValue } from "./types.js";

export interface FileOptions {
	required?: boolean;
	maxSize?: number; // In bytes
	allowedTypes?: string[]; // MIME types
	helpText?: string;
}

/**
 * File field - file upload
 */
export function file(options: FileOptions = {}): FieldDefinition<FileValue> {
	const fileObjSchema = z.object({
		id: z.string(),
		url: z.string(),
		filename: z.string(),
		mimeType: z.string(),
		size: z.number(),
	});

	// Optional vs required
	const schema: z.ZodTypeAny = options.required ? fileObjSchema : fileObjSchema.optional();

	const ui: FieldUIHints = {
		widget: "file",
		helpText: options.helpText,
		maxSize: options.maxSize,
		allowedTypes: options.allowedTypes,
	};

	return {
		type: "file",
		columnType: "TEXT",
		schema,
		options,
		ui,
	};
}
