import { z } from 'zod';

export const idSchema = z.number().int().positive();
const text = (max = 300) => z.string().trim().min(1, 'Wajib diisi').max(max, `Maksimal ${max} karakter`);
export const dateSchema = z.string().regex(/^\d{4}-\d{2}-\d{2}$/, 'Tanggal tidak valid').refine(value => {
  const date = new Date(`${value}T00:00:00Z`);
  return !value.startsWith('0000') && Number.isFinite(date.getTime()) && date.toISOString().slice(0, 10) === value;
}, 'Tanggal tidak valid');
export const roomSchema = z.object({ code: text(30).regex(/^[A-Za-z0-9_-]+$/, 'Gunakan huruf, angka, - atau _').transform(v => v.toUpperCase()), name: text() });
export const speakerSchema = z.object({ name: text(), institution: z.string().trim().max(300).nullable().optional() });
export const eventSchema = z.object({ name: text(), startDate: dateSchema, endDate: dateSchema }).refine(v => v.endDate >= v.startDate, { path: ['endDate'], message: 'Tanggal selesai harus setelah atau sama dengan tanggal mulai' });
export const eventRoomSchema = z.object({ eventId: idSchema, roomId: idSchema, category: z.string().trim().max(300).nullable().optional() });
export const categorySchema = eventRoomSchema.pick({ category: true });
export const topicSchema = z.object({ title: text(2000), speakerIds: z.array(idSchema).refine(ids => new Set(ids).size === ids.length, 'Narasumber tidak boleh berulang') });
export const sessionSchema = z.object({ eventRoomId: idSchema, title: text(1000), startsAt: z.iso.datetime({ offset: true }), endsAt: z.iso.datetime({ offset: true }), topics: z.array(topicSchema) }).refine(v => new Date(v.endsAt) > new Date(v.startsAt), { path: ['endsAt'], message: 'Waktu selesai harus setelah waktu mulai' });
export const emailSchema = z.string().trim().toLowerCase().pipe(z.email('Email tidak valid').max(320));
export const passwordSchema = z.string().min(12, 'Kata sandi minimal 12 karakter').max(256);
export const loginSchema = z.object({ email: emailSchema, password: z.string().min(1).max(256) });
export const profileSchema = z.object({ name: text(), currentPassword: z.string().max(256).optional(), newPassword: z.union([z.literal(''), passwordSchema]).optional() }).refine(v => !v.newPassword || !!v.currentPassword, { path: ['currentPassword'], message: 'Masukkan kata sandi saat ini' });
export type SessionInput = z.infer<typeof sessionSchema>;
export type EventInput = z.infer<typeof eventSchema>;
export type EventDto = { id: number; name: string; startDate: string; endDate: string };
export type RoomDto = { id: number; code: string; name: string };
export type SpeakerDto = { id: number; name: string; institution: string | null };
export type EventRoomDto = { id: number; eventId: number; roomId: number; category: string | null; event: EventDto; room: RoomDto };
export type TopicDto = { id: number; title: string; position: number; speakers: SpeakerDto[] };
export type SessionDto = { id: number; eventRoomId: number; title: string; startsAt: string; endsAt: string; topics: TopicDto[] };
export type ScheduleDto = { event: EventDto; room: RoomDto; category: string | null; sessions: SessionDto[] };
export type AdminDto = { id: number; name: string; email: string };
export type AuthDto = { user: AdminDto | null; csrfToken: string };
export type PageDto<T> = { items: T[]; total: number; page: number; pageSize: number };
