/**
 * OCCP System — shared domain types.
 *
 * These mirror the database schema in `db/schema.sqlite.sql` /
 * `db/schema.postgres.sql`. Booleans are stored as 0/1 integers so the same SQL
 * runs on SQLite (local prototype) and PostgreSQL (Supabase deployment).
 */

export const ROLES = ["SUPER_ADMIN", "DEAN_ADMIN", "ENCODER", "VIEWER"] as const;
export type Role = (typeof ROLES)[number];

export const ROLE_LABELS: Record<Role, string> = {
  SUPER_ADMIN: "Super Administrator",
  DEAN_ADMIN: "Dean / Academic Administrator",
  ENCODER: "Encoder / Academic Staff",
  VIEWER: "Viewer",
};

export const CURRICULUM_STATUSES = ["DRAFT", "DRAFT_REVIEW", "PUBLISHED", "ARCHIVED"] as const;
export type CurriculumStatus = (typeof CURRICULUM_STATUSES)[number];

export const ASSESSMENT_STATUSES = [
  "NOT_TAKEN",
  "IN_PROGRESS",
  "PASSED",
  "CREDITED",
  "FAILED",
  "FOR_VALIDATION",
] as const;
export type AssessmentStatus = (typeof ASSESSMENT_STATUSES)[number];

/** Statuses whose curriculum units are counted toward completion. */
export const CREDITING_STATUSES: readonly AssessmentStatus[] = ["PASSED", "CREDITED"];

export const ASSIGNMENT_METHODS = ["AUTO", "MANUAL", "UNRESOLVED"] as const;
export type AssignmentMethod = (typeof ASSIGNMENT_METHODS)[number];

export const RULE_TYPES = ["PREFIX", "REGEX", "EXACT", "NUMERIC_RANGE"] as const;
export type RuleType = (typeof RULE_TYPES)[number];

export const YEAR_LONG_POLICIES = ["ON_FINAL_PASS", "PER_COMPONENT"] as const;
export type YearLongCreditPolicy = (typeof YEAR_LONG_POLICIES)[number];

export const NON_UNIT_REQUIREMENT_TYPES = [
  "SUMMER_TRAINING",
  "CLERKSHIP_CORE",
  "CLERKSHIP_ELECTIVE",
] as const;
export type NonUnitRequirementType = (typeof NON_UNIT_REQUIREMENT_TYPES)[number];

export const NON_UNIT_PROGRESS_STATUSES = [
  "NOT_STARTED",
  "IN_PROGRESS",
  "COMPLETED",
  "WAIVED",
] as const;
export type NonUnitProgressStatus = (typeof NON_UNIT_PROGRESS_STATUSES)[number];

export const OCCP_YEAR_LEVELS = ["1st Year", "2nd Year", "3rd Year", "4th Year"] as const;
export type OccpYearLevel = (typeof OCCP_YEAR_LEVELS)[number];

export const VALIDATION_SEVERITIES = ["ERROR", "WARNING", "INFO"] as const;
export type ValidationSeverity = (typeof VALIDATION_SEVERITIES)[number];

export const IMPORT_TYPES = ["CURRICULUM", "STUDENT", "ASSESSMENT"] as const;
export type ImportType = (typeof IMPORT_TYPES)[number];

export const IMPORT_MODES = ["CREATE_ONLY", "UPDATE_EXISTING_BY_STUDENT_NUMBER"] as const;
export type ImportMode = (typeof IMPORT_MODES)[number];

export interface Program {
  id: string;
  code: string;
  name: string;
  active: number;
  notes: string | null;
  created_at: string;
  updated_at: string;
}

export interface Profile {
  id: string;
  email: string;
  full_name: string;
  role: Role;
  active: number;
  /** NULL grants access to every program; otherwise the user is scoped to it. */
  program_id: string | null;
  created_at: string;
  updated_at: string;
}

export interface Curriculum {
  id: string;
  code: string;
  name: string;
  effective_year: number | null;
  version: string;
  status: CurriculumStatus;
  year_long_credit_policy: YearLongCreditPolicy;
  institutional_type: string | null;
  source_name: string | null;
  source_workbook: string | null;
  primary_effective_ay: string | null;
  secondary_sheet_effective_ay: string | null;
  mapping_status: string | null;
  source_total_units_declared: number | null;
  clerkship_reference_days: number | null;
  notes: string | null;
  program_id: string | null;
  published_at: string | null;
  published_by: string | null;
  created_at: string;
  updated_at: string;
}

export interface CurriculumCourse {
  id: string;
  curriculum_id: string;
  course_code: string;
  course_code_key: string;
  course_title: string;
  units: number;
  lecture_units: number | null;
  laboratory_units: number | null;
  hours_per_week: number | null;
  intended_year: number;
  term: string;
  category: string | null;
  prerequisite_text: string | null;
  required: number;
  counts_toward_program_units: number;
  is_year_long_component: number;
  year_long_group_id: string | null;
  component_term: string | null;
  final_grade_term: string | null;
  sort_order: number;
  active: number;
  notes: string | null;
  source_sheet: string | null;
  source_row: number | null;
  source_notes: string | null;
  created_at: string;
  updated_at: string;
}

export interface NonUnitRequirement {
  id: string;
  curriculum_id: string;
  requirement_type: NonUnitRequirementType;
  code: string | null;
  title: string;
  academic_year: number | null;
  term: string | null;
  required_hours: number | null;
  required_days: number | null;
  required_months: number | null;
  elective: number;
  elective_group: string | null;
  minimum_selections: number | null;
  prerequisite_text: string | null;
  sort_order: number;
  active: number;
  source_sheet: string | null;
  source_row: number | null;
  notes: string | null;
  created_at: string;
  updated_at: string;
}

export interface CurriculumAssignmentRule {
  id: string;
  name: string;
  curriculum_id: string;
  rule_type: RuleType;
  prefix: string | null;
  regex_pattern: string | null;
  exact_student_number: string | null;
  numeric_start: number | null;
  numeric_end: number | null;
  priority: number;
  active: number;
  description: string | null;
  created_at: string;
  updated_at: string;
}

export interface Student {
  id: string;
  student_number: string;
  student_number_key: string;
  last_name: string;
  first_name: string;
  middle_name: string | null;
  suffix: string | null;
  premed_course: string | null;
  premed_school: string | null;
  graduation_year: number | null;
  gwa: string | null;
  nmat_score: number | null;
  nmat_year: number | null;
  curriculum_id: string | null;
  curriculum_assignment_method: AssignmentMethod;
  curriculum_assignment_rule_id: string | null;
  curriculum_override_reason: string | null;
  active: number;
  notes: string | null;
  created_at: string;
  updated_at: string;
  last_assessed_at: string | null;
  updated_by: string | null;
  program_id: string | null;
}

export interface StudentCourseAssessment {
  id: string;
  student_id: string;
  curriculum_course_id: string;
  grade_raw: string | null;
  grade_numeric: number | null;
  status: AssessmentStatus;
  equivalent_source_school: string | null;
  equivalent_course_code: string | null;
  equivalent_course_title: string | null;
  equivalent_source_units: number | null;
  approved_credited_units_override: number | null;
  remarks: string | null;
  updated_by: string | null;
  created_at: string;
  updated_at: string;
}

export interface StudentNonUnitProgress {
  id: string;
  student_id: string;
  non_unit_requirement_id: string;
  selected: number;
  status: NonUnitProgressStatus;
  completed_hours: number | null;
  completed_months: number | null;
  remarks: string | null;
  updated_by: string | null;
  created_at: string;
  updated_at: string;
}

export interface AuditLog {
  id: string;
  actor_user_id: string | null;
  actor_email: string | null;
  action: string;
  entity_type: string;
  entity_id: string | null;
  before_json: string | null;
  after_json: string | null;
  reason: string | null;
  created_at: string;
}

export interface ImportJob {
  id: string;
  import_type: ImportType;
  file_name: string;
  status: string;
  total_rows: number;
  accepted_rows: number;
  rejected_rows: number;
  uploaded_by: string | null;
  error_summary_json: string | null;
  created_at: string;
  completed_at: string | null;
}

export interface CurriculumValidationIssue {
  id: string;
  curriculum_id: string;
  code: string;
  severity: ValidationSeverity;
  message: string;
  details_json: string | null;
  acknowledged: number;
  acknowledged_by: string | null;
  acknowledged_at: string | null;
  created_at: string;
}

export interface SystemSetting {
  key: string;
  value: string;
  updated_at: string;
  updated_by: string | null;
}
