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

extendZodWithOpenApi(z);

// ---------------------------------------------------------------------------
// Enums
// ---------------------------------------------------------------------------

export const SystemConfigGroupEnum = z.enum([
	'BILLING',
	'GENERAL',
	'INCIDENT',
	'PAYMENT',
	'BRAND',
]);

// ---------------------------------------------------------------------------
// Entity schema
// ---------------------------------------------------------------------------

export const SystemConfigSchema = z
	.object({
		id: z.string().uuid(),
		config_key: z.string(),
		config_group: SystemConfigGroupEnum,
		description: z.string().nullable(),
		config_value: z.record(z.string(), z.unknown()),
		is_active: z.boolean(),
		created_at: z.string().datetime(),
		updated_at: z.string().datetime(),
	})
	.openapi('SystemConfig');

// ---------------------------------------------------------------------------
// Request schemas
// ---------------------------------------------------------------------------

export const SystemConfigCreateBodySchema = z
	.object({
		config_key: z.string().min(3).max(100),
		config_group: SystemConfigGroupEnum,
		description: z.string().optional(),
		config_value: z.record(z.string(), z.unknown()),
		is_active: z.boolean().default(true).optional(),
	})
	.openapi('SystemConfigCreateBody');

export const SystemConfigUpdateBodySchema = z
	.object({
		config_key: z.string().min(3).max(100).optional(),
		config_group: SystemConfigGroupEnum.optional(),
		description: z.string().optional().nullable(),
		config_value: z.record(z.string(), z.unknown()).optional(),
		is_active: z.boolean().optional(),
	})
	.openapi('SystemConfigUpdateBody');

// ---------------------------------------------------------------------------
// Response schemas
// ---------------------------------------------------------------------------

export const SystemConfigResponseSchema = ApiResponseSchema(SystemConfigSchema).openapi(
	'SystemConfigResponse',
);

export const SystemConfigListResponseSchema = ApiResponseSchema(
	z.object({
		count: z.number(),
		rows: z.array(SystemConfigSchema),
		page: z.number(),
		pageSize: z.number(),
	}),
).openapi('SystemConfigListResponse');

// ---------------------------------------------------------------------------
// Query schemas
// ---------------------------------------------------------------------------

export const SystemConfigQueryParamsSchema = z
	.object({
		filters: z.string().optional().openapi({
			description: 'filter, visit https://www.npmjs.com/package/sequelize-api-paginate for syntax',
		}),
		sortField: z.string().optional().openapi({
			description: 'sortField, visit https://www.npmjs.com/package/sequelize-api-paginate for syntax',
		}),
		sortOrder: z.enum(['asc', 'desc']).optional().openapi({
			description: 'sort order, visit https://www.npmjs.com/package/sequelize-api-paginate for syntax',
		}),
		page: z.coerce.number().int().positive().default(1).openapi({
			description: 'page, visit https://www.npmjs.com/package/sequelize-api-paginate for syntax',
		}),
		pageSize: z.coerce.number().int().positive().max(100).default(10).openapi({
			description: 'pageSize, visit https://www.npmjs.com/package/sequelize-api-paginate for syntax',
		}),
	})
	.openapi('SystemConfigQueryParams');
