/**
 * Local types for the classrooms/meetings feature area.
 *
 * A shared `resources/js/types/easyform.ts` is expected to eventually export
 * the same shapes (Classroom, Participant, Meeting, Attendance, MeetingEvent,
 * FormatempSync, Paginated<T>). Until that file exists these local
 * definitions let this feature compile independently; field names are kept
 * in sync with the backend contract so this file can be deleted in favor of
 * the shared one later without touching call sites (beyond the import path).
 */

export type PaginationLink = {
    url: string | null;
    label: string;
    active: boolean;
};

export type Paginated<T> = {
    data: T[];
    links: PaginationLink[];
    current_page?: number;
    last_page?: number;
    per_page?: number;
    total?: number;
    from?: number | null;
    to?: number | null;
};

export type ClassroomStatus = 'draft' | 'active' | 'closed';

export type Classroom = {
    id: number;
    uuid: string;
    code: string;
    name: string;
    course_title: string | null;
    formatemp_project_code: string | null;
    formatemp_project_type_id?: string | null;
    starts_on: string | null;
    ends_on: string | null;
    status: ClassroomStatus;
    max_participants: number | null;
    max_hours: number | null;
    /** Crediti addebitati per quest'aula. */
    credits_cost?: number | null;
    record_meetings: boolean;
    max_cameras: number | null;
    moderator_name: string | null;
    moderator_email: string | null;
    access_password: string | null;
    notes: string | null;
    created_at: string;
    participants_count: number;
    meetings_count: number;
};

export type ParticipantRole = 'learner' | 'trainer';

export type Participant = {
    id: number;
    classroom_id: number;
    first_name: string;
    last_name: string;
    full_name: string;
    fiscal_code: string | null;
    birth_date: string | null;
    gender: string | null;
    birth_place_code: string | null;
    email: string | null;
    phone: string | null;
    role: ParticipantRole;
    join_token: string;
    formatemp_id: string | null;
    /** `ParPro_`: il partecipante nel progetto Forma.Temp. */
    formatemp_learner_id?: string | null;
};

/** One row of the AI import preview, before the user confirms it. */
export type AiParsedParticipant = {
    first_name: string;
    last_name: string;
    fiscal_code: string | null;
    email: string | null;
    phone: string | null;
    role: ParticipantRole;
    birth_date: string | null;
    gender: string | null;
    birth_place_code: string | null;
    warnings: string[];
    /** False when the codice fiscale fails the strict check: l'import rifiuterebbe la riga. */
    importable?: boolean;
};

export type AiParticipantsResponse = {
    rows: AiParsedParticipant[];
    warnings: string[];
};

/** One lesson of the calendar proposed by the assistant. */
export type AiPlannedMeeting = {
    title: string;
    scheduled_at: string;
    duration_minutes: number;
};

export type AiSchedule = {
    meetings: AiPlannedMeeting[];
    warnings: string[];
};

export type MeetingAnomalyType =
    | 'below_threshold'
    | 'partial'
    | 'disconnections'
    | 'late_join'
    | 'trainer_absent';

export type MeetingAnomaly = {
    type: MeetingAnomalyType;
    participant_id: number | null;
    participant: string | null;
    detail: string;
    percent?: number;
    minutes?: number;
    count?: number;
};

export type MeetingReport = {
    anomalies: MeetingAnomaly[];
    note: string;
    generated_at: string;
    model: string;
};

export type MeetingStatus = 'scheduled' | 'running' | 'ended';

export type Meeting = {
    id: number;
    uuid: string;
    classroom_id: number;
    title: string;
    scheduled_at: string;
    duration_minutes: number;
    status: MeetingStatus;
    bbb_internal_id: string | null;
    record: boolean;
    participants_count: number;
    started_at: string | null;
    ended_at: string | null;
    ai_report?: MeetingReport | null;
    /** Fascia oraria Forma.Temp a cui la lezione è collegata. */
    formatemp_fascia_id?: string | null;
    formatemp_register_id?: string | null;
    formatemp_sent_at?: string | null;
    /** Consolidata la fascia, il registro su Forma.Temp non accetta altri invii. */
    formatemp_consolidated_at?: string | null;
};

export type Attendance = {
    id: number;
    joined_at: string;
    left_at: string | null;
    seconds: number;
};

export type MeetingEvent = {
    id: number;
    event: string;
    bbb_user_id: string | null;
    participant_id: number | null;
    occurred_at: string;
    payload: Record<string, unknown> | null;
};

export type FormatempSyncType =
    | 'classroom'
    | 'participants'
    | 'attendance'
    | 'connection'
    | 'fasce'
    | 'presenza'
    | 'personale'
    | 'consolida'
    | 'discente';
export type FormatempSyncStatus = 'pending' | 'running' | 'success' | 'failed';

export type FormatempSync = {
    id: number;
    type: FormatempSyncType;
    status: FormatempSyncStatus;
    error: string | null;
    explanation: string | null;
    attempts: number;
    synced_at: string | null;
    created_at: string;
};
