/** أنواع البيانات المشتركة بين الواجهة الأمامية والخلفية */

export interface StudentWithStatus {
  id: string;
  studentCode: string;
  nameAr: string;
  nameEn: string;
  grade: string;
  classroom: string;
  guardianName: string;
  guardianPhone: string;
  guardianAddress?: string | null;
  notes?: string | null;
  totalFee: number;
  /** نسبة الخصم (0-100) */
  discountPercent?: number;
  /** القسط الصافي بعد الخصم */
  effectiveFee?: number;
  feeDueDate?: string | null;
  /** تاريخ التسجيل الفعلي (أساس حساب التقسيط) — قد يختلف عن تاريخ الإدخال للنظام */
  registeredAt?: string;
  createdAt: string;
  paid: number;
  remaining: number;
  paymentsCount?: number;
}

export interface PaymentDTO {
  id: string;
  receiptNumber: string;
  studentId: string;
  student?: { nameAr: string; nameEn: string; studentCode: string; classroom: string; grade: string; totalFee?: number; discountPercent?: number };
  amount: number;
  installmentType: string;
  method: string;
  transactionRef?: string | null;
  notes?: string | null;
  paidAt: string;
  createdBy?: string | null;
  creator?: { fullName: string } | null;
  /** بيانات حالة السداد لإعادة طباعة الوصل (تُحسب في API المدفوعات) */
  paidTotal?: number;
  remaining?: number;
  /** القسط الصافي بعد الخصم (من API المدفوعات) */
  effectiveFee?: number;
}

export interface ExpenseDTO {
  id: string;
  category: string;
  description: string;
  amount: number;
  invoiceName?: string | null;
  invoiceData?: string | null;
  spentAt: string;
  createdAt: string;
  creator?: { fullName: string } | null;
}

/** إيراد آخر غير أقساط الطلبة (تبرعات، أنشطة، كافتيريا...) */
export interface OtherIncomeDTO {
  id: string;
  source: string;
  reason: string;
  amount: number;
  notes?: string | null;
  receivedAt: string;
  createdAt: string;
  creator?: { fullName: string } | null;
}

export interface SchoolSettingsDTO {
  id: string;
  nameAr: string;
  nameEn: string;
  logoData?: string | null;
  address?: string | null;
  phone?: string | null;
  email?: string | null;
  currency: string;
  footerText?: string | null;
}

/** المدرسة (v8 متعدد المدارس) */
export interface SchoolDTO {
  id: string;
  name: string;
  nameEn?: string | null;
  address?: string | null;
  phone?: string | null;
  isActive: boolean;
  createdAt: string;
  studentsCount?: number;
  usersCount?: number;
  isDefault?: boolean;
}

export interface UserDTO {
  id: string;
  username: string;
  fullName: string;
  phone?: string | null;
  role: string;
  isActive: boolean;
  /** المدرسة المرتبطة بالحساب (v8) */
  schoolId?: string | null;
  schoolName?: string | null;
  /** الصفحات المسموحة — null = الافتراضي حسب الدور */
  allowedViews?: string[] | null;
  createdAt: string;
  /** تاريخ حذف الحساب (حذف منطقي) — غير محذوف = null */
  deletedAt?: string | null;
}

/** حساب محذوف (حذف منطقي) — يظهر في قسم الحسابات المحذوفة للإدارة */
export interface DeletedUserDTO {
  id: string;
  username: string;
  fullName: string;
  role: string;
  deletedAt: string;
}

export interface AuditLogDTO {
  id: string;
  userId?: string | null;
  userName?: string | null;
  role?: string | null;
  action: string;
  entity: string;
  entityId?: string | null;
  details?: string | null;
  createdAt: string;
}

export interface AnnouncementDTO {
  id: string;
  message: string;
  level: string;
  isActive: boolean;
  createdAt: string;
  creator?: { fullName: string } | null;
}

export interface DashboardData {
  stats: {
    studentsCount: number;
    totalIncome: number;
    totalExpenses: number;
    netBalance: number;
    monthIncome: number;
    monthExpenses: number;
    todayIncome: number;
    outstanding: number;
  };
  lateStudents: { id: string; nameAr: string; studentCode: string; grade: string; remaining: number; feeDueDate?: string | null }[];
  recentPayments: PaymentDTO[];
  recentExpenses: ExpenseDTO[];
  announcements: AnnouncementDTO[];
  monthlyChart: { month: string; income: number; expenses: number }[];
}

export interface ReportRow {
  id?: string;
  date: string;
  type: "income" | "expense";
  /** نوع الإيراد: قسط طالب أو إيراد آخر (v8) */
  incomeKind?: "installment" | "other";
  studentCode?: string;
  reference: string;
  studentName?: string;
  category?: string;
  description: string;
  installmentType?: string;
  method?: string;
  amount: number;
  by: string;
}

export interface ReportData {
  rows: ReportRow[];
  totals: {
    income: number;
    expenses: number;
    net: number;
    incomeCount: number;
    expenseCount: number;
    /** تفصيل الإيرادات (v8): أقساط الطلبة مقابل الإيرادات الأخرى */
    installmentIncome?: number;
    otherIncome?: number;
  };
  settings: SchoolSettingsDTO;
  periodLabel: string;
}

/** صف في تقرير حالة سداد الطلبة (مسدد | مستحق | متبقي) */
export interface StudentStatusRow {
  id: string;
  studentCode: string;
  nameAr: string;
  grade: string;
  classroom: string;
  guardianPhone: string;
  /** تاريخ التسجيل الفعلي (أساس حساب التقسيط) */
  registeredAt: string;
  createdAt: string;
  /** القسط الصافي بعد الخصم */
  netFee: number;
  paid: number;
  /** المطلوب حتى الآن حسب جدول الـ 8 أشهر */
  expectedToDate: number;
  /** المستحق الآن غير المسدد */
  dueNow: number;
  remaining: number;
  monthsElapsed: number;
  deadline: string;
  /** تجاوز الموعد النهائي (نهاية نيسان من السنة الجديدة) مع وجود متبقي */
  pastDeadline: boolean;
  status: "PAID" | "DUE" | "REMAINING";
}

export interface StudentsStatusReport {
  asOf: string;
  students: StudentStatusRow[];
  summary: {
    totalCount: number;
    paidCount: number;
    dueCount: number;
    remainingCount: number;
    lateCount: number;
    totalPaid: number;
    totalDueNow: number;
    totalRemaining: number;
  };
  installmentMonths: number;
}
