import { z } from 'zod';
import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi';
import { ApiResponseSchema } from '#contracts/shared';

extendZodWithOpenApi(z);

// Request body schemas for validation and OpenAPI documentation
export const TechnicianCreateBodySchema = z.object({
	// User account fields
	first_name: z.string().min(1).max(100).openapi({ example: 'Nguyễn' }),
	last_name: z.string().min(1).max(100).optional().openapi({ example: 'Văn A' }),
	phone: z.string().min(1).max(20).openapi({ example: '0901234567' }),
	email: z.string().email().optional().nullable().openapi({ example: 'technician@example.com' }),
	// Technician-specific fields
	specialty: z.string().min(1).max(100).openapi({ example: 'Điện' }),
	status: z.enum(['active', 'inactive', 'on_leave', 'terminated']).optional().default('active'),
	avatar_id: z.uuid().optional().nullable(),
	notes: z.string().optional().nullable(),
}).openapi('TechnicianCreateBody');

export const TechnicianUpdateBodySchema = z.object({
	specialty: z.string().min(1).max(100).optional(),
	status: z.enum(['active', 'inactive', 'on_leave', 'terminated']).optional(),
	avatar_id: z.uuid().optional().nullable(),
	notes: z.string().optional().nullable(),
}).openapi('TechnicianUpdateBody');

// Nested user info returned with technician
// (Removed - user info is now flattened into technician response)

// Response schemas for OpenAPI documentation
export const TechnicianSchema = z.object({
	id: z.uuid(),
	user_id: z.uuid().nullable().optional(),
	full_name: z.string().nullable().optional().openapi({ example: 'Nguyễn Văn A' }),
	phone: z.string().nullable().optional().openapi({ example: '0901234567' }),
	email: z.string().nullable().optional().openapi({ example: 'tech@example.com' }),
	specialty: z.string(),
	status: z.enum(['active', 'inactive', 'on_leave', 'terminated']),
	avatar_id: z.uuid().nullable().optional(),
	notes: z.string().nullable().optional(),
	created_at: z.iso.datetime(),
	updated_at: z.iso.datetime().nullable(),
	deleted_at: z.iso.datetime().nullable(),
}).openapi('Technician');

export const TechnicianListDataSchema = z.object({
	count: z.number(),
	rows: z.array(TechnicianSchema),
	page: z.number(),
	pageSize: z.number(),
}).openapi('TechnicianListData');

export const TechnicianResponseSchema = ApiResponseSchema(TechnicianSchema).openapi('TechnicianResponse');

export const TechnicianListResponseSchema = ApiResponseSchema(TechnicianListDataSchema).openapi('TechnicianListResponse');
