import type { Prisma } from '../generated/prisma/client.js';
import type { EventDto, EventRoomDto, RoomDto, SessionDto, SpeakerDto } from '../shared/contracts.js';

export const sessionInclude = { topics: { orderBy: { position: 'asc' }, include: { topicSpeakers: { orderBy: { position: 'asc' }, include: { speaker: true } } } } } satisfies Prisma.SessionInclude;
export const eventRoomInclude = { event: true, room: true } satisfies Prisma.EventRoomInclude;
export function eventDto(e: { id: number; name: string; startDate: Date; endDate: Date }): EventDto {
  return { id: e.id, name: e.name, startDate: e.startDate.toISOString().slice(0, 10), endDate: e.endDate.toISOString().slice(0, 10) };
}
export function roomDto(r: RoomDto): RoomDto { return { id: r.id, code: r.code, name: r.name }; }
export function speakerDto(s: SpeakerDto): SpeakerDto { return { id: s.id, name: s.name, institution: s.institution }; }
export function eventRoomDto(a: Prisma.EventRoomGetPayload<{ include: typeof eventRoomInclude }>): EventRoomDto {
  return { id: a.id, eventId: a.eventId, roomId: a.roomId, category: a.category, event: eventDto(a.event), room: roomDto(a.room) };
}
export function sessionDto(s: Prisma.SessionGetPayload<{ include: typeof sessionInclude }>): SessionDto {
  return { id: s.id, eventRoomId: s.eventRoomId, title: s.title, startsAt: s.startsAt.toISOString(), endsAt: s.endsAt.toISOString(), topics: s.topics.map(t => ({ id: t.id, title: t.title, position: t.position, speakers: t.topicSpeakers.map(l => speakerDto(l.speaker)) })) };
}
