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

extendZodWithOpenApi(z);

// ---------------------------------------------------------------------------
// Shared
// ---------------------------------------------------------------------------

const TARGET_TYPES = ['all', 'building', 'apartment', 'floor', 'room'] as const;
const STATUSES = ['draft', 'published', 'archived'] as const;

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

export const PostCreateBodySchema = z
	.object({
		title: z.string().min(1).max(255),
		content: z.string().optional(),
		thumbnailId: z.string().uuid().optional().nullable(),
		targetType: z.enum(TARGET_TYPES),
		targetIds: z.array(z.string().uuid()).default([]),
		isFeatured: z.boolean().default(false),
		status: z.enum(STATUSES).default('draft'),
		publishedAt: z.string().datetime().optional().nullable(),
	})
	.refine(
		(v) => v.targetType === 'all' || v.targetIds.length > 0,
		{ message: 'targetIds must not be empty when targetType is not "all"', path: ['targetIds'] },
	)
	.openapi('PostCreateBody');

export const PostUpdateBodySchema = z
	.object({
		title: z.string().min(1).max(255).optional(),
		content: z.string().optional().nullable(),
		thumbnailId: z.string().uuid().optional().nullable(),
		targetType: z.enum(TARGET_TYPES).optional(),
		targetIds: z.array(z.string().uuid()).optional(),
		isFeatured: z.boolean().optional(),
		status: z.enum(STATUSES).optional(),
		publishedAt: z.string().datetime().optional().nullable(),
	})
	.openapi('PostUpdateBody');

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

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

export const PostListItemSchema = z.object({
	id: z.string().uuid(),
	title: z.string(),
	thumbnail_id: z.string().uuid().nullable(),
	target_type: z.string(),
	status: z.string(),
	is_featured: z.boolean(),
	author_id: z.string().uuid(),
	author_name: z.string().nullable(),
	published_at: z.string().datetime().nullable(),
	created_at: z.string().datetime(),
	updated_at: z.string().datetime(),
}).openapi('PostListItem');

export const PostDetailSchema = z.object({
	id: z.string().uuid(),
	title: z.string(),
	content: z.string().nullable(),
	thumbnail_id: z.string().uuid().nullable(),
	thumbnail: FileSchema.nullable(),
	target_type: z.string(),
	target_ids: z.array(z.string().uuid()),
	status: z.string(),
	is_featured: z.boolean(),
	author_id: z.string().uuid(),
	author: PostAuthorSchema.nullable(),
	published_at: z.string().datetime().nullable(),
	created_at: z.string().datetime(),
	updated_at: z.string().datetime(),
}).openapi('PostDetail');

export const PostListDataSchema = z.object({
	count: z.number(),
	rows: z.array(PostListItemSchema),
	page: z.number(),
	pageSize: z.number(),
}).openapi('PostListData');

export const PostResponseSchema = ApiResponseSchema(PostDetailSchema).openapi('PostResponse');
export const PostListResponseSchema = ApiResponseSchema(PostListDataSchema).openapi('PostListResponse');
