export type JobOpeningStatus = 'draft' | 'open' | 'closed';

export type ApplicationStatus =
  | 'applied'
  | 'screening'
  | 'interview'
  | 'assessment'
  | 'hold'
  | 'offer'
  | 'hired'
  | 'rejected'
  | 'withdrawn';

export type InterviewStatus =
  | 'pending_schedule'
  | 'scheduled'
  | 'in_progress'
  | 'completed'
  | 'cancelled'
  | 'rescheduled';

export type InterviewType = 'screening' | 'technical' | 'behavioral' | 'panel' | 'final';

export type InterviewLocationType = 'virtual' | 'onsite' | 'phone';

export type ApplicantSource =
  | 'direct_application'
  | 'referral'
  | 'linkedin'
  | 'job_board'
  | 'recruitment_agency'
  | 'career_fair'
  | 'internal'
  | 'other';

export type ReferralStatus = 'pending' | 'applicant_created' | 'interviewed' | 'hired' | 'rejected';

// ── Contract Types ──────────────────────────────────────────

export interface ContractType {
  id: string;
  name: string;
  description: string | null;
  is_active: boolean;
  created_at: string;
  updated_at: string;
}

export interface CreateContractTypePayload {
  name: string;
  description?: string | null;
  is_active?: boolean;
}

export interface UpdateContractTypePayload extends Partial<CreateContractTypePayload> {
  id: string;
}

// ── Job Openings ────────────────────────────────────────────

export interface JobOpening {
  id: string;
  title: string;
  description: string | null;
  requirements: string | null;
  key_responsibilities: string[];
  skills_and_experience: string[];
  department_id: string | null;
  branch_id: string | null;
  location: string | null;
  contract_type_id: string | null;
  number_of_positions: number;
  salary_min: number | null;
  salary_max: number | null;
  salary_currency: string | null;
  show_salary_range: boolean;
  expected_attachments: string[];
  hiring_manager_id: string | null;
  status: JobOpeningStatus;
  is_published: boolean;
  referral_bonus_eligible: boolean;
  referral_bonus_amount: number | null;
  workflow_instance_id: string | null;
  expires_at: string | null;
  department?: { id: string; name: string } | null;
  branch?: { branch_id: string; name: string } | null;
  contract_type?: ContractType | null;
  hiring_manager?: { employee_id: string; firstname: string; lastname: string } | null;
  applicants_count?: number;
  interviews_count?: number;
  created_at: string;
  updated_at: string;
}

export interface CreateJobOpeningPayload {
  title: string;
  description?: string | null;
  key_responsibilities?: string[];
  skills_and_experience?: string[];
  department_id?: string | null;
  branch_id?: string | null;
  contract_type_id?: string | null;
  number_of_positions?: number;
  salary_min?: number | null;
  salary_max?: number | null;
  show_salary_range?: boolean;
  expected_attachments?: string[];
  hiring_manager_id?: string | null;
  status?: JobOpeningStatus;
  expires_at?: string | null;
}

export interface UpdateJobOpeningPayload extends Partial<CreateJobOpeningPayload> {
  id: string;
}

// ── Applicants ──────────────────────────────────────────────

export interface ApplicantAttachment {
  type: string;
  path: string;
  original_name?: string;
  size?: number;
  custom_label?: string;
}

export type RejectionReason =
  | 'salary_mismatch'
  | 'overqualified'
  | 'timing'
  | 'role_filled_internally'
  | 'strong_candidate_wrong_role'
  | 'withdrew_accepted_offer'
  | 'withdrew_other'
  | 'performance_concern'
  | 'culture_concern';

export interface ApplicantApplication {
  id: string;
  applicant_id: string;
  job_opening_id: string;
  status: ApplicationStatus;
  applied_date: string | null;
  expected_salary_min: number | null;
  expected_salary_max: number | null;
  notes: string | null;
  rejection_reason?: RejectionReason;
  rejection_notes?: string;
  job_opening?: JobOpening;
  created_at: string;
  updated_at: string;
}

export interface Applicant {
  id: string;
  first_name: string;
  last_name: string;
  full_name?: string;
  email: string;
  phone: string | null;
  address: string | null;
  city: string | null;
  region: string | null;
  country: string | null;
  resume_path: string | null;
  attachments: ApplicantAttachment[] | null;
  linkedin_url: string | null;
  portfolio_url: string | null;
  years_of_experience: number | null;
  current_company: string | null;
  current_position: string | null;
  notice_period: number | null;
  source: ApplicantSource;
  notes: string | null;
  employee_referral_id: string | null;
  job_openings?: (JobOpening & { pivot: ApplicantApplication })[];
  created_at: string;
  updated_at: string;
}

export interface CreateApplicantPayload {
  first_name: string;
  last_name: string;
  email: string;
  phone?: string | null;
  address?: string | null;
  city?: string | null;
  country?: string | null;
  linkedin_url?: string | null;
  portfolio_url?: string | null;
  years_of_experience?: number | null;
  current_company?: string | null;
  current_position?: string | null;
  notice_period?: number | null;
  source: ApplicantSource;
  notes?: string | null;
  job_opening_ids?: string[];
  expected_salary_min?: number | null;
  expected_salary_max?: number | null;
  employee_referral_id?: string | null;
}

export interface AdvanceApplicationPayload {
  applicantId: string;
  applicationId: string;
  new_status: ApplicationStatus;
}

export interface AttachFilePayload {
  applicantId: string;
  type: string;
  file: File;
}

export interface DetachFilePayload {
  applicantId: string;
  type: string;
  path: string;
  delete_file?: boolean;
}

// ── Interviews ──────────────────────────────────────────────

export interface SkillsAssessment {
  [criterionName: string]: number;
}

export interface Interview {
  id: string;
  applicant_id: string;
  job_opening_id: string;
  type: InterviewType;
  status: InterviewStatus;
  scheduled_at: string | null;
  duration_minutes: number | null;
  location_type: InterviewLocationType | null;
  location_details: string | null;
  interviewer_id: string | null;
  panel_interviewers: string[];
  feedback: string | null;
  overall_rating: number | null;
  skills_assessment: SkillsAssessment | null;
  recommend_for_hiring: boolean | null;
  strengths: string | null;
  concerns: string | null;
  cancellation_reason: string | null;
  completed_at: string | null;
  workflow_instance_id: string | null;
  applicant?: Applicant;
  job_opening?: JobOpening;
  interviewer?: { user_id: string; name?: string; employee?: { firstname: string; lastname: string } } | null;
  created_at: string;
  updated_at: string;
}

export interface CreateInterviewPayload {
  applicant_id: string;
  job_opening_id: string;
  type: InterviewType;
  scheduled_at?: string | null;
  duration_minutes?: number | null;
  location_type?: InterviewLocationType | null;
  location_details?: string | null;
  interviewer_id?: string | null;
  panel_interviewers?: string[];
}

export interface ScheduleInterviewPayload {
  scheduled_at: string;
  duration_minutes: number;
  location_type: InterviewLocationType;
  location_details?: string | null;
  interviewer_id?: string | null;
  panel_interviewers?: string[];
}

export interface SubmitFeedbackPayload {
  feedback: string;
  overall_rating: 1 | 2 | 3 | 4 | 5;
  skills_assessment?: SkillsAssessment;
  recommend_for_hiring: boolean;
  strengths?: string | null;
  concerns?: string | null;
  completed_at?: string;
}

export interface GenerateTokenPayload {
  interviewId: string;
  email: string;
  full_name: string;
  expiration_hours?: number;
}

export interface ExternalInterviewerToken {
  token: string;
  email: string;
  full_name: string;
  expires_at: string;
  access_url: string;
}

// ── Interview Criteria ──────────────────────────────────────

export interface InterviewCriterion {
  id: string;
  name: string;
  description: string | null;
  weight: number;
  is_default: boolean;
  job_opening_id: string | null;
  created_at: string;
  updated_at: string;
}

export interface CreateInterviewCriterionPayload {
  name: string;
  description?: string | null;
  weight?: number;
  job_opening_id?: string | null;
}

// ── Referrals ───────────────────────────────────────────────

export interface Referral {
  id: string;
  referrer_employee_id: string;
  referred_first_name: string;
  referred_last_name: string;
  referred_email: string;
  referred_phone: string | null;
  job_opening_id: string;
  qualification_notes: string;
  relationship_to_candidate: string | null;
  notes: string | null;
  status: ReferralStatus;
  converted_applicant_id: string | null;
  referrer?: { employee_id: string; firstname: string; lastname: string };
  job_opening?: JobOpening;
  created_at: string;
  updated_at: string;
}

export interface CreateReferralPayload {
  referred_first_name: string;
  referred_last_name: string;
  referred_email: string;
  referred_phone?: string | null;
  job_opening_id: string;
  qualification_notes: string;
  relationship_to_candidate?: string | null;
  notes?: string | null;
}

// ── Approvals ───────────────────────────────────────────────

export type RecruitmentApprovalStatus =
  | 'pending'
  | 'approved'
  | 'rejected'
  | 'delegated'
  | 'cancelled'
  | 'sent_back';

export interface RecruitmentApprovalInstance {
  id: string;
  entity_type: string;
  entity_id: string;
  status: RecruitmentApprovalStatus;
  requester_id: string;
  current_approver_id: string | null;
  notes: string | null;
  entity?: JobOpening | null;
  created_at: string;
  updated_at: string;
}

export interface ApprovalActionPayload {
  instanceId: string;
  notes?: string;
}

export interface DelegateApprovalPayload {
  instanceId: string;
  delegate_to: string;
  notes?: string;
}

// ── Analytics ───────────────────────────────────────────────

export interface SourceStatistic {
  source: ApplicantSource;
  count: number;
  percentage: number;
}

export interface SourceAnalytics {
  sources: SourceStatistic[];
  total: number;
}

// ── Talent Pool ─────────────────────────────────────────────

export interface TalentMatch {
  id: string;
  applicant_id: string;
  job_opening_id: string;
  rejection_reason: RejectionReason;
  rejection_notes?: string;
  applied_date: string;
  updated_at: string;
  expected_salary_min?: number;
  expected_salary_max?: number;
  applicant: Applicant;
  job_opening: JobOpening;
  interview_score?: number;
}
