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

extendZodWithOpenApi(z);

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

export const NotificationTemplateTypeEnum = z.enum([
	'MARKETING',
	'BILLING',
	'SYSTEM_ALERT',
	'TRANSACTIONAL',
	'REMINDER',
	'ANNOUNCEMENT',
]);

export const NotificationTemplateChannelEnum = z.enum([
	'SMS',
	'EMAIL',
	'ZALO_ZNS',
	'ZALO_OA',
	'APP_PUSH',
	'IN_APP',
	'WEB_PUSH',
]);

export const NotificationTemplateStatusEnum = z.enum(['ACTIVE', 'INACTIVE', 'DRAFT']);

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

export const NotificationTemplateSchema = z
	.object({
		id: z.string().uuid(),
		code: z.string(),
		name: z.string(),
		type: NotificationTemplateTypeEnum,
		channel: NotificationTemplateChannelEnum,
		content: z.string(),
		subject: z.string().nullable(),
		description: z.string().nullable(),
		variables: z.array(z.string()).nullable(),
		status: NotificationTemplateStatusEnum,
		created_by: z.string().uuid().nullable(),
		updated_by: z.string().uuid().nullable(),
		created_at: z.string().datetime(),
		updated_at: z.string().datetime(),
	})
	.openapi('NotificationTemplate');

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

export const NotificationTemplateCreateBodySchema = z
	.object({
		// code là optional — nếu không truyền sẽ tự sinh từ name
		code: z.string().min(3).max(50).optional(),
		name: z.string().min(1).max(255),
		type: NotificationTemplateTypeEnum,
		channel: NotificationTemplateChannelEnum,
		content: z.string().min(1),
		subject: z.string().max(500).optional(),
		description: z.string().optional(),
		variables: z.array(z.string()).optional(),
		status: NotificationTemplateStatusEnum.default('ACTIVE').optional(),
	})
	.openapi('NotificationTemplateCreateBody');

export const NotificationTemplateUpdateBodySchema = z
	.object({
		name: z.string().min(1).max(255).optional(),
		type: NotificationTemplateTypeEnum.optional(),
		channel: NotificationTemplateChannelEnum.optional(),
		content: z.string().min(1).optional(),
		subject: z.string().max(500).optional().nullable(),
		description: z.string().optional().nullable(),
		variables: z.array(z.string()).optional().nullable(),
		status: NotificationTemplateStatusEnum.optional(),
	})
	.openapi('NotificationTemplateUpdateBody');

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

export const NotificationTemplateResponseSchema = ApiResponseSchema(NotificationTemplateSchema).openapi(
	'NotificationTemplateResponse',
);

export const NotificationTemplateListResponseSchema = ApiResponseSchema(
	z.object({
		count: z.number(),
		rows: z.array(NotificationTemplateSchema),
		page: z.number(),
		pageSize: z.number(),
	}),
).openapi('NotificationTemplateListResponse');
