export interface Branding {
  name: string;
  logo_url?: string;
  favicon_url?: string | null;
  primary_color: string;
  secondary_color: string;
  portal_title: string;
  admin_title: string;
  font_family?: string;
}

export interface GradeLevel {
  id: number;
  name: string;
  education_stage_id?: number;
  order?: number;
}

export interface EducationStage {
  id: number;
  name: string;
  grade_levels?: GradeLevel[];
}

export interface StudyFieldOption {
  id: number;
  name: string;
  code?: string | null;
  order?: number;
}

export interface SubjectFieldCoefficientRow {
  study_field_id: number;
  study_field_name?: string | null;
  coefficient: number;
  assigned?: boolean;
}

export interface SubjectRow {
  id: number;
  name: string;
  code?: string | null;
  image_url?: string | null;
  type: string;
  subject_scope?: "general" | "specialized";
  coefficient: number;
  field_coefficients?: SubjectFieldCoefficientRow[];
  assigned_study_field_ids?: number[];
  grade_level_id?: number | null;
  curriculum_book_id?: number | null;
  branch_id?: number | null;
  is_org_subject?: boolean;
  is_branch_subject?: boolean;
  can_edit?: boolean;
  is_active: boolean;
  curriculum_book?: { id: number; title: string } | null;
  grade_level?: { id: number; name: string } | null;
  branch?: { id: number; name: string } | null;
}

export interface StudyFieldMatrixGrade {
  id: number;
  name: string;
  order: number;
  has_study_fields: boolean;
  /** Full catalog for this stage (org admin toggles per grade). */
  catalog_fields?: StudyFieldOption[];
  /** Fields enabled at org level for this grade (branch UI uses this). */
  study_fields: StudyFieldOption[];
  selected_study_field_ids: number[];
}

export interface StudyFieldMatrixStage {
  id: number;
  name: string;
  slug?: string | null;
  catalog_fields?: StudyFieldOption[];
  grade_levels: StudyFieldMatrixGrade[];
}

export interface AcademicYear {
  id: number;
  title: string;
  is_current: boolean;
  is_active?: boolean;
  starts_at?: string;
  ends_at?: string;
}

export interface BranchItem {
  id: number;
  name: string;
  code: string;
  settings?: Record<string, unknown>;
  is_active: boolean;
}

export interface SeatQuotaItem {
  id: number;
  branch_id: number;
  target_type: string;
  target_id: number;
  academic_year_id: number;
  max_seats: number;
  reserved_seats: number;
  waitlist_max?: number | null;
}

export interface ApplicationDetail {
  id: number;
  uuid: string;
  status: string;
  applicant_national_code: string;
  is_foreign_national?: boolean;
  submitted_at?: string | null;
  queue_type?: string | null;
  waitlist_position?: number | null;
  branch?: {
    id: number;
    name: string;
    profile?: {
      city?: string | null;
      address?: string | null;
      phone?: string | null;
      website?: string | null;
    };
  } | null;
  grade_level?: { id: number; name: string } | null;
  study_field_id?: number | null;
  study_field?: { id: number; name: string } | null;
  sis_student_profile_id?: number | null;
  sis_enrollment_id?: number | null;
  staff_notes?: string | null;
  form_data?: Record<string, unknown>;
  educational_fields?: Array<{
    key: string;
    label: string;
    type?: string;
    options?: Array<{ value: string; label: string }>;
  }>;
  required_documents?: Array<{ type: string; label: string; required?: boolean }>;
  documents?: Array<{
    id: number;
    document_type: string;
    original_name: string;
    label?: string;
    url?: string;
    download_url?: string;
    is_image?: boolean;
  }>;
  payments?: Array<{
    id: number;
    method: string;
    method_label?: string;
    gateway?: string;
    gateway_label?: string;
    amount: number;
    currency?: string;
    status: string;
    status_label?: string;
    reference?: string | null;
    order_id?: string | null;
    paid_at?: string | null;
    created_at?: string | null;
    receipt_url?: string | null;
    receipt_download_url?: string | null;
    receipt_is_image?: boolean;
    receipt_name?: string | null;
  }>;
  evaluations?: Array<{
    id: number;
    step_type: string;
    status: string;
    score?: number | null;
    notes?: string | null;
  }>;
  status_logs?: Array<{ id: number; to_status: string; note?: string | null; created_at: string }>;
}

export const DEFAULT_RULES: Record<string, unknown> = {
  enabled: true,
  allowed_genders: ["male", "female"],
  birth_date_from: null,
  birth_date_to: null,
  min_age: null,
  max_age: null,
  min_previous_gpa: null,
  min_previous_gpa_mode: "warn",
  rules_text: "",
  rules_html: "",
  required_documents: [],
  educational_form_fields: [],
  parent_form_fields: [],
  commitment_forms: [],
  admission_fee: { free: true, amount: 0, currency: "IRR" },
  payment_methods: ["online", "card_to_card"],
  card_to_card_info: { bank: "", card: "", holder: "" },
  seat_quota: null,
  waitlist_enabled: true,
  waitlist_max: null,
  evaluation_steps: ["document_review"],
  followup: {
    mode: "contact_later",
    contact_message: "",
    exam: {
      title: "",
      address: "",
      notes: "",
      map: null,
      schedule_title: "",
      starts_at: null,
      ends_at: null,
    },
  },
};
