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 IncidentCreateBodySchema = z.object({
	title: z.string().min(1).max(200), // TODO: adjust validation rules (type, min, max, etc.)
	description: z.string().min(1).max(200), // TODO: adjust validation rules (type, min, max, etc.)
	type_id: z.string().min(1).max(200), // TODO: adjust validation rules (type, min, max, etc.)
	room_id: z.string().min(1).max(200), // TODO: adjust validation rules (type, min, max, etc.)
	reporter_id: z.string().min(1).max(200), // TODO: adjust validation rules (type, min, max, etc.)
	technician_id: z.string().min(1).max(200).optional().nullable(), // TODO: adjust validation rules (type, min, max, etc.)
	priority: z.string().min(1).max(200).optional(), // TODO: adjust validation rules (type, min, max, etc.)
	status: z.string().min(1).max(200).optional(), // TODO: adjust validation rules (type, min, max, etc.)
}).openapi('IncidentCreateBody');

export const IncidentUpdateBodySchema = z.object({
	title: z.string().min(1).max(200).optional(), // TODO: adjust validation rules (type, min, max, etc.)
	description: z.string().min(1).max(200).optional(), // TODO: adjust validation rules (type, min, max, etc.)
	type_id: z.string().min(1).max(200).optional(), // TODO: adjust validation rules (type, min, max, etc.)
	room_id: z.string().min(1).max(200).optional(), // TODO: adjust validation rules (type, min, max, etc.)
	reporter_id: z.string().min(1).max(200).optional(), // TODO: adjust validation rules (type, min, max, etc.)
	technician_id: z.string().min(1).max(200).optional(), // TODO: adjust validation rules (type, min, max, etc.)
	priority: z.string().min(1).max(200).optional(), // TODO: adjust validation rules (type, min, max, etc.)
	status: z.string().min(1).max(200).optional(), // TODO: adjust validation rules (type, min, max, etc.)
}).openapi('IncidentUpdateBody');

// Optimized response schemas (for list and detail endpoints)
export const IncidentListItemSchema = z.object({
	id: z.string().uuid(),
	title: z.string(),
	code: z.string().nullable(),
	type_id: z.string().uuid(),
	room_id: z.string().uuid(),
	reporter_id: z.string().uuid(),
	technician_id: z.string().uuid().nullable(),
	type_name: z.string().nullable(),
	room_name: z.string().nullable(),
	apartment_name: z.string().nullable(),
	building_name: z.string().nullable(),
	reporter_name: z.string().nullable(),
	technician_name: z.string().nullable(),
	priority: z.string(),
	status: z.string(),
	deadline: z.string().datetime().nullable(),
	created_at: z.string().datetime(),
	updated_at: z.string().datetime().nullable(),
}).openapi('IncidentListItem');

// Nested schemas for detail response
export const ReporterDetailSchema = z.object({
	id: z.string().uuid(),
	name: z.string().nullable(),
	phone: z.string().nullable(),
	email: z.string().nullable(),
}).openapi('ReporterDetail');

export const TechnicianDetailSchema = z.object({
	id: z.string().uuid(),
	name: z.string().nullable(),
	phone: z.string().nullable(),
	email: z.string().nullable(),
}).openapi('TechnicianDetail');

// Detail response schema (includes description)
export const IncidentDetailSchema = z.object({
	id: z.string().uuid(),
	title: z.string(),
	code: z.string().nullable(),
	description: z.string().nullable(),
	type_id: z.string().uuid(),
	room_id: z.string().uuid(),
	type_name: z.string().nullable(),
	room_name: z.string().nullable(),
	apartment_name: z.string().nullable(),
	building_name: z.string().nullable(),
	reporter: ReporterDetailSchema.nullable(),
	technician: TechnicianDetailSchema.nullable(),
	priority: z.string(),
	status: z.string(),
	deadline: z.string().datetime().nullable(),
	created_at: z.string().datetime(),
	updated_at: z.string().datetime().nullable(),
}).openapi('IncidentDetail');

export const IncidentListDataSchema = z.object({
	count: z.number(),
	rows: z.array(IncidentListItemSchema),
	page: z.number(),
	pageSize: z.number(),
}).openapi('IncidentListData');

export const IncidentResponseSchema = ApiResponseSchema(IncidentDetailSchema).openapi('IncidentResponse');

export const IncidentListResponseSchema = ApiResponseSchema(IncidentListDataSchema).openapi('IncidentListResponse');

