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

extendZodWithOpenApi(z);

// Bill item schema
export const BillItemSchema = z.object({
	id: z.string().uuid(),
	bill_id: z.string().uuid(),
	fee_name: z.string().min(1).max(255),
	quantity: z.number().int().min(1).default(1),
	unit_price: z.number().nonnegative(),
	created_at: z.string().datetime(),
}).openapi('BillItem');

// Bill item schema for creation requests (no id/bill_id/created_at required)
export const BillItemCreateSchema = z.object({
	fee_name: z.string().min(1).max(255),
	quantity: z.number().int().min(1).default(1).optional(),
	unit_price: z.number().nonnegative(),
}).openapi('BillItemCreate');

// Request body schemas for validation and OpenAPI documentation
export const BillCreateBodySchema = z.object({
	room_id: z.string().uuid(),
	resident_id: z.string().uuid(),
	billing_period: z.string().regex(/^\d{4}-\d{2}$/), // yyyy-mm format
	due_date: z.string(), // DATE format
	payment_method: z.string().max(50).optional(),
	notes: z.string().optional(),
	total_amount: z.number().nonnegative().optional(),
	status: z.enum(['draft', 'paid', 'pending', 'overdue']).default('pending'),
	bill_items: z.array(BillItemCreateSchema).min(1),
}).openapi('BillCreateBody');

export const BillUpdateBodySchema = z.object({
	room_id: z.string().uuid().optional(),
	resident_id: z.string().uuid().optional(),
	billing_period: z.string().regex(/^\d{4}-\d{2}$/).optional(),
	due_date: z.string().optional(),
	payment_method: z.string().max(50).optional(),
	notes: z.string().optional(),
	total_amount: z.number().nonnegative().optional(),
	status: z.enum(['draft', 'paid', 'pending', 'overdue']).optional(),
}).openapi('BillUpdateBody');

// Response schemas
export const BillDetailSchema = z.object({
	id: z.string().uuid(),
	code: z.string(),
	room_id: z.string().uuid(),
	resident_id: z.string().uuid(),
	billing_period: z.string(),
	due_date: z.string(),
	payment_method: z.string().nullable(),
	notes: z.string().nullable(),
	total_amount: z.number().nullable(),
	status: z.string(),
	created_by: z.string().uuid().nullable(),
	created_at: z.string().datetime(),
	room_name: z.string().nullable(),
	apartment_name: z.string().nullable(),
	building_name: z.string().nullable(),
	resident_full_name: z.string().nullable(),
	resident_email: z.string().nullable(),
	resident_phone: z.string().nullable(),
	bill_items: z.array(BillItemSchema).optional(),
	updated_at: z.string().datetime().nullable(),
	deleted_by: z.string().uuid().nullable(),
	deleted_at: z.string().datetime().nullable(),
}).openapi('BillDetail');

export const BillListItemSchema = BillDetailSchema.openapi('BillListItem');

export const BillListDataSchema = z.object({
	count: z.number(),
	rows: z.array(BillListItemSchema),
	page: z.number(),
	pageSize: z.number(),
}).openapi('BillListData');

export const BillResponseSchema = ApiResponseSchema(BillDetailSchema).openapi('BillResponse');

export const BillListResponseSchema = ApiResponseSchema(BillListDataSchema).openapi('BillListResponse');
