import type { CampusId, CampusInfo } from '@/shared/types/app';
import type { ScopeTier } from '@/business/scope/selectors';
import type { StaffAttendanceStatus } from '@/shared/types/staffAttendance';
import type { StaffAttendanceSummaryViewModel } from '@/business/staff-attendance/types';
import type { TenantLevel } from '@/shared/types/scope';

export type AttendanceScopeMode = 'organization' | 'school' | 'campus';

export interface CampusAttendanceConfigViewModel {
  readonly id: string;
  readonly campus_id: CampusId;
  readonly attendance_link: string | null;
  readonly updated_by: string | null;
  readonly updated_at: string;
}

export interface CampusAttendanceSummaryViewModel {
  readonly id: string;
  readonly campus_id: CampusId;
  readonly date: string;
  readonly total_enrolled: number;
  readonly total_present: number;
  readonly total_absent: number;
  readonly total_tardy: number;
  readonly attendance_percentage: number;
  readonly recorded_by: string | null;
  readonly notes: string | null;
}

export interface CampusAttendanceEntryInput {
  readonly campusId: CampusId;
  readonly date: string;
  readonly totalEnrolled: number;
  readonly totalPresent: number;
  readonly totalAbsent: number;
  readonly totalTardy: number;
  readonly notes: string | null;
}

export interface CampusAttendanceStats extends CampusInfo {
  readonly todayPct: number | null;
  readonly weekAvg: number | null;
  readonly config: CampusAttendanceConfigViewModel | null;
  readonly recentData: readonly CampusAttendanceSummaryViewModel[];
  readonly todayRecord: CampusAttendanceSummaryViewModel | null;
  readonly recentStaffData: readonly StaffAttendanceDailySummaryViewModel[];
  readonly todayStaffRecord: StaffAttendanceDailySummaryViewModel | null;
}

export interface CampusAttendanceChildStats {
  readonly id: string;
  readonly level: TenantLevel;
  readonly mascot: string;
  readonly fullName: string;
  readonly bgGradient: string;
  readonly isOnline?: boolean;
  readonly todayPct: number | null;
  readonly weekAvg: number | null;
  readonly recentData: readonly CampusAttendanceSummaryViewModel[];
  readonly todayRecord: CampusAttendanceSummaryViewModel | null;
  readonly childCampusIds: readonly CampusId[];
  readonly recentStaffData: readonly StaffAttendanceDailySummaryViewModel[];
  readonly todayStaffRecord: StaffAttendanceDailySummaryViewModel | null;
}

export interface CampusAttendanceOverallStats {
  readonly todayRecords: readonly CampusAttendanceSummaryViewModel[];
  readonly todayEnrolled: number;
  readonly todayPresent: number;
  readonly todayPct: number | null;
  readonly weekRecords: readonly CampusAttendanceSummaryViewModel[];
  readonly weekPct: number | null;
}

export interface CampusAttendanceEntryDraft {
  readonly date: string;
  readonly campusId: CampusId;
  readonly enrolled: string;
  readonly present: string;
  readonly absent: string;
  readonly tardy: string;
  readonly notes: string;
}

export interface StaffAttendanceEntryDraft {
  readonly date: string;
  readonly userId: string;
  readonly status: StaffAttendanceStatus;
  readonly note: string;
}

export interface AttendanceRosterUser {
  readonly id: string;
  readonly name: string;
  readonly role: string | null;
}

export type AttendanceRosterStatus = StaffAttendanceStatus;

export interface CampusAttendanceRollupDraft {
  readonly campusId: CampusId;
  readonly campusName: string;
  readonly enrolled: string;
  readonly present: string;
  readonly absent: string;
  readonly tardy: string;
  readonly notes: string;
  readonly hasRecordedData: boolean;
}

export interface CampusAttendanceRoleAccess {
  readonly isSuperintendent: boolean;
  readonly isDirector: boolean;
  readonly isOfficeManager: boolean;
  readonly canSeeAllCampuses: boolean;
  readonly canEnterData: boolean;
  readonly canPrint: boolean;
  readonly canReadStaffReports: boolean;
}

export interface CampusAttendanceScopeModel {
  readonly tier: ScopeTier;
  readonly mode: AttendanceScopeMode;
  readonly title: string;
  readonly description: string;
  readonly aggregateHelper: string;
  readonly reportLabel: string;
  readonly emptyHistoryMessage: string;
  readonly campusId: CampusId | null;
  readonly tenantName: string | null;
}

export interface CampusAttendanceCombinedStats {
  readonly studentTodayPct: number | null;
  readonly studentWeekPct: number | null;
  readonly staffTodayPct: number | null;
  readonly combinedTodayPct: number | null;
}

export interface StaffAttendanceDailySummaryViewModel {
  readonly id: string;
  readonly campus_id: CampusId | null;
  readonly date: string;
  readonly total_staff: number;
  readonly total_present: number;
  readonly total_absent: number;
  readonly total_late: number;
  readonly attendance_percentage: number;
  readonly notes: string | null;
}

export type CombinedAttendanceGroup = 'students' | 'staff' | 'total';

export interface CombinedAttendanceHistoryRow {
  readonly id: string;
  readonly date: string;
  readonly group: CombinedAttendanceGroup;
  readonly label: string;
  readonly total: number;
  readonly present: number;
  readonly absent: number;
  readonly late: number;
  readonly attendancePercentage: number | null;
  readonly notes: string | null;
}

export interface CampusAttendanceStaffSummaryState {
  readonly summary: StaffAttendanceSummaryViewModel | null;
  readonly loading: boolean;
  readonly error: unknown;
}

export interface CampusAttendancePrintInput {
  readonly reportTitle: string;
  readonly generatedByName: string;
  readonly generatedByRole: string;
  readonly today: string;
  readonly weekStart: string;
  readonly campusesToPrint: readonly CampusAttendanceStats[];
  readonly printTodayRecords: readonly CampusAttendanceSummaryViewModel[];
  readonly printWeekRecords: readonly CampusAttendanceSummaryViewModel[];
  readonly staffSummary: StaffAttendanceSummaryViewModel | null;
}
