// app/admin/adminTypes.ts

export type Role = 'Admin' | 'Staff' | 'Viewer';

export interface AppUser {
    id: string;
    name: string;
    role: Role;
}

export interface Youth {
    id: string;
    name: string;
    team: 'PF' | 'TU';
}

export interface Period {
    id: string;
    name: string;
    startDate: string;
    endDate: string;
    youthList: Youth[];
}

export interface AttendanceEntry {
    hoursWorked: number;
    weightedHours: number;
    status: 'Absent' | 'Late' | 'Present' | 'Pending';
    note?: string;
    date: string;
    youthId: string;
    shiftId: 'MORNING' | 'AFTERNOON';
}

export type AttendanceDataMap = Record<string, AttendanceEntry>;

export const toIsoDate = (date: Date) => {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
};

export const isWeekend = (dateStr: string): boolean => {
    const day = new Date(dateStr + 'T12:00:00').getDay();
    return day === 0 || day === 6;
};

export const calculateShiftDuration = (timeStr?: string): number => {
    if (!timeStr || timeStr === 'Ledig') return 0;
    const matches = timeStr.match(/(\d{1,2}):(\d{2})/g);
    if (!matches || matches.length < 2) return 0;
    const parse = (t: string) => {
        const [h, m] = t.split(':').map(Number);
        return h + (m / 60);
    };
    return parse(matches[1]) - parse(matches[0]);
};

export const getAttendanceKey = (date: string, youthId: string, shiftId: string) => `${date}|${youthId}|${shiftId}`;

export const formatTimeHHMM = (decimalHours: number): string => {
    if (decimalHours <= 0) return "0:00";
    const hrs = Math.floor(decimalHours);
    const mins = Math.round((decimalHours - hrs) * 60);
    return `${hrs}:${mins.toString().padStart(2, '0')}`;
};

export const parseTimeInput = (input: string): number => {
    if (!input) return 0;
    const cleanInput = input.trim().replace(',', '.');

    if (cleanInput.includes(':')) {
        const [h, m] = cleanInput.split(':').map(Number);
        return (h || 0) + ((m || 0) / 60);
    }

    return parseFloat(cleanInput) || 0;
};