import type {
  ApplicantStatus,
  Candidate as DBCandidate,
  CandidateDocument,
  CandidateEducation,
  CandidateExperience,
  CandidateLanguage,
  CandidateLicense,
  DB,
  DocumentStatus,
  GermanState,
  LanguageCode,
  LanguageLevel,
  NurseRecognitionGermany,
  NurseRecognitionHomeCountry,
  ProfessionalRecognitionStatus,
  RecognitionDossierDocumentCategory,
  RecognitionDossierStatus,
  RecognitionLikelihood,
} from './db.js'

export type CountriesMap = Record<string, string>

export interface ApplicationCandidateInfo {
  stateWorkPreference: GermanState[] | null
  joinTalentPool: boolean | null
  livingInGermany: boolean | null
  euPassport: boolean | null
  countryEducation: string | null
  submittedAt: Date | null
  languagesSpoken: LanguageCode[] | null
  nationality: string | null
  currentResidence: string | null
  professionalRecognitionStatus: ProfessionalRecognitionStatus | null
  qualificationStatusGermany: NurseRecognitionGermany | null
  recognitionLikelihood: RecognitionLikelihood | null
}

export type ApplicationCandidateLanguage = Pick<
  DBCandidate,
  'languageCertificateProvided' | 'languageSelfAssessmentLevel'
> & {
  languageCertificates: CandidateDocument[]
}

export interface ApplicationCandidateLicense {
  licenses: CandidateDocument[]
  licenseProvided: boolean | null
}

export interface ApplicationCandidateEducation {
  education: CandidateDocument[]
}

export interface ApplicationCandidateWorkExperience {
  workExperience: CandidateDocument[]
}

export type PublicCandidateExperience = {
  employerName: string | null
  roleTitle: string | null
  department: string | null
  country: string | null
  city: string | null
  facilityType: string | null
  startDate: Date | null
  endDate: Date | null
}

export interface PublicCandidateEducationSummary {
  institution: string | null
  program: string | null
  country: string | null
  completionDate: Date | null
  nursingRelatedDegree: boolean | null
}

export interface PublicCandidateInfo {
  candidateId: string
  name: string
  stateWorkPreference: GermanState[] | null
  joinTalentPool: boolean | null
  livingInGermany: boolean | null
  euPassport: boolean | null
  countryEducation: string | null
  submittedAt: Date | null
  languagesSpoken: LanguageCode[] | null
  nationality: string | null
  currentResidence: string | null
  professionalRecognitionStatus: ProfessionalRecognitionStatus | null
  qualificationStatusGermany: NurseRecognitionGermany | null
  recognitionLikelihood: RecognitionLikelihood | null
  languageSelfAssessmentLevel: LanguageLevel | null
  languageCertificateProvided: boolean | null
  qualificationStatusHomeCountry: NurseRecognitionHomeCountry | null
  createdAt: Date | null
  age: number | null
  hasVerifiedLicense: boolean
  profileImageUrl: string | null
  totalExperienceMonths: number | null
  experience: PublicCandidateExperience[]
  education: PublicCandidateEducationSummary | null
}

export interface ApplicationCandidateData {
  name: string
  candidateId: string
  dateOfBirth: Date | null
  submittedAt: Date | null
  status: ApplicantStatus
  stateWorkPreference: GermanState[] | null
  joinTalentPool: boolean | null
  livingInGermany: boolean | null
  euPassport: boolean | null
  countryEducation: string | null
  languagesSpoken: LanguageCode[] | null
  nationality: string | null
  currentResidence: string | null
  professionalRecognitionStatus: ProfessionalRecognitionStatus | null
  qualificationStatusGermany: NurseRecognitionGermany | null
  recognitionLikelihood: RecognitionLikelihood | null
  languageCertificateProvided: boolean | null
  languageSelfAssessmentLevel: LanguageLevel | null
  languageCertificates: CandidateDocument[]
  education: CandidateDocument[]
  licenses: CandidateDocument[]
  workExperience: CandidateDocument[]
}

export type QualificationCardStatus =
  | 'completed'
  | 'required'
  | 'in-progress'
  | 'optional'

export type QualificationCard = {
  title: string
  description: string
  status: QualificationCardStatus
  progress?: number // 0-100 only when status === 'in-progress'
  requirements: string[]
  steps?: string[]
}

export type NextStepItem = {
  title: string
  subtitle: string
  costs: string
  button?: string
  buttonimprove?: string
  buttonjoin?: string
}

export type NextSteps = {
  gethelp: NextStepItem
  connect: NextStepItem
  documents?: NextStepItem
  submit?: NextStepItem
  compensatory?: NextStepItem
  german?: NextStepItem
}

export type CandidateResults = {
  education: QualificationCard
  license: QualificationCard
  language: QualificationCard
  experience: QualificationCard
  copies: QualificationCard
  originals: QualificationCard
  joinTalentPool: boolean | null
  nextSteps: NextSteps | null
  textContent: {
    renderPath: string | null
    hero: string
    result: string
    explanation: string[]
    recognition?: string[]
    notes: {
      afterExplanation: string[]
    }
  }
}

export interface CandidateLifecycleAnalysisNotification {
  issues?: string[]
  updated_at?: string
  updatedAt?: string
  [key: string]: unknown
}

export type CandidateLifecycleNotifications = Record<string, unknown> & {
  analysis?: CandidateLifecycleAnalysisNotification
}

export interface BackOfficeDocuments {
  analysis: {
    education: CandidateEducation | null
    license: CandidateLicense | null
    language: CandidateLanguage | null
    experience: CandidateExperience | null
  }
  documents: {
    education: Array<CandidateDocument>
    license: Array<CandidateDocument>
    language: Array<CandidateDocument>
    experience: Array<CandidateDocument>
  }
}

export interface BackOfficeCandidateListItem {
  candidateId: string
  email: string
  name: string
  stateWorkPreference: GermanState[] | null
  joinTalentPool: boolean | null
  livingInGermany: boolean | null
  euPassport: boolean | null
  countryEducation: string | null
  licenseProvided: boolean | null
  languageCertificateProvided: boolean | null
  languageSelfAssessmentLevel: LanguageLevel | null
  submittedAt: Date | null
  createdAt: Date | null
  lastUpdatedAt: Date | null
  phone: string | null
  dateOfBirth: Date | null
  currentResidence: string | null
  qualificationStatusHomeCountry: NurseRecognitionHomeCountry | null
  qualificationStatusGermany: NurseRecognitionGermany | null
  licenseMandatory: boolean | null
  durationRequirementsMet: boolean | null
  status: ApplicantStatus
  incompleteReminderSentAt: Date | null
  recognitionLikelihood: RecognitionLikelihood | null
  professionalRecognitionStatus: ProfessionalRecognitionStatus | null
  nationality: string | null
  languagesSpoken: LanguageCode[] | null
  statusChangedAt: Date
  analysisLastRunAt: Date | null
  analysisInputFingerprint: string | null
  invalidatedAt: Date | null
  reuploadRequestedAt: Date | null
  reuploadedAt: Date | null
  failedPurgedAt: Date | null
}

export interface BackOfficeCandidate {
  candidateId: string
  email: string
  name: string
  stateWorkPreference: GermanState[] | null
  joinTalentPool: boolean | null
  livingInGermany: boolean | null
  euPassport: boolean | null
  countryEducation: string | null
  licenseProvided: boolean | null
  languageCertificateProvided: boolean | null
  languageSelfAssessmentLevel: LanguageLevel | null
  submittedAt: Date | null
  createdAt: Date | null
  lastUpdatedAt: Date | null
  phone: string | null
  dateOfBirth: Date | null
  currentResidence: string | null
  qualificationStatusHomeCountry: NurseRecognitionHomeCountry | null
  qualificationStatusGermany: NurseRecognitionGermany | null
  licenseMandatory: boolean | null
  durationRequirementsMet: boolean | null
  status: ApplicantStatus
  incompleteReminderSentAt: Date | null
  recognitionLikelihood: RecognitionLikelihood | null
  professionalRecognitionStatus: ProfessionalRecognitionStatus | null
  nationality: string | null
  languagesSpoken: LanguageCode[] | null
  statusChangedAt: Date
  analysisLastRunAt: Date | null
  analysisInputFingerprint: string | null
  invalidatedAt: Date | null
  reuploadRequestedAt: Date | null
  reuploadedAt: Date | null
  failedPurgedAt: Date | null
  analysis: {
    education: CandidateEducation | null
    license: CandidateLicense | null
    language: CandidateLanguage | null
    experience: CandidateExperience | null
  }
  documents: {
    education: Array<CandidateDocument>
    license: Array<CandidateDocument>
    language: Array<CandidateDocument>
    experience: Array<CandidateDocument>
  }
  lifecycleNotifications?: CandidateLifecycleNotifications | null
}

export interface RecognitionDossierBlockingIssue {
  code: string
  message: string
}

export interface RecognitionDossierChecklistItem {
  id: string
  label: string
  status: string
  message: string | null
  source: string | null
  sourceLabel: string | null
  required: boolean
  notes: string[]
}

export interface RecognitionDossierSubmissionGuide {
  authorityName: string | null
  authorityWebsiteUrl: string | null
  submissionMode: string | null
  submissionUrl: string | null
  postalAddress: string[]
  contactEmail: string | null
  contactPhone: string | null
  notes: string[]
  lastVerifiedAt: string | null
}

export interface RecognitionDossierDocumentSummary {
  documentId: string
  candidateId: string
  dossierId: string
  category: RecognitionDossierDocumentCategory
  title: string | null
  note: string | null
  fileName: string
  fileSize: number | null
  mimeType: string | null
  uploaded: boolean
  documentStatus: DocumentStatus
  createdAt: Date | null
  lastUpdatedAt: Date | null
}

export interface RecognitionDossierSummary {
  dossierId: string
  candidateId: string
  targetState: GermanState
  targetProfession: NurseRecognitionGermany
  status: RecognitionDossierStatus
  rulesVersion: string | null
  generatedAt: Date | null
  lastUpdatedAt: Date | null
  hasPackage: boolean
  blockingIssues: RecognitionDossierBlockingIssue[]
}

export interface RecognitionDossierDetail extends RecognitionDossierSummary {
  supported: boolean
  packageFilePath: string | null
  downloadUrl: string | null
  submissionGuide: RecognitionDossierSubmissionGuide | null
  checklist: RecognitionDossierChecklistItem[]
  documents: RecognitionDossierDocumentSummary[]
}

export interface GetCandidateResult {
  name: string
  qualificationStatusGermany: NurseRecognitionGermany | null
  qualificationStatusHomeCountry: NurseRecognitionHomeCountry | null
  euPassport: boolean | null
  livingInGermany: boolean | null
  licenseMandatory: boolean | null
  joinTalentPool: boolean | null
  nursingRelatedDegree: boolean | null
  accreditedUniversity: boolean | null
  accreditedStudyProgram: boolean | null
  docs: {
    cv: DocumentStatus
    language: DocumentStatus
    license: DocumentStatus
  }
  structuredTraining: {
    hasDuration: boolean | null
    isNursingFocus: boolean | null
    hasTheoryHours: boolean | null
    hasPracticeHours: boolean | null
    overallStatus: boolean | null
  }
  languageAssessmentType: 'certificate' | 'self_assessment' | null
  isValid: boolean
  languageLevel?: LanguageLevel | null
}

export interface GetBackOfficeCandidatesOutput {
  total: number
  data: BackOfficeCandidateListItem[]
}

export interface GetCompanyCandidatesOutput {
  total: number
  data: PublicCandidateInfo[]
}

export type BackendTranslationNamespace = 'cards' | 'misc' | 'outputs'

export interface BackendTranslationBundle {
  locale: string
  namespace: BackendTranslationNamespace
  content: Record<string, unknown>
  updatedAt: Date
}
