export * from "../fb_collections";
import { IProfile } from "../../evo-people";
import type {
  FirestoreDocumentReference,
  IAddress,
  IContact,
  IExternalLink,
  ITag,
} from "../../shared";
import { IMedication, PatientAttachmentBaseCategory } from "../dic";

// Permissões para o médico
export const EvoMedProfessionalPermissions = {
  List: "evo_med_people_professional_read",
  Get: "evo_med_people_professional_read",
  Create: "evo_med_people_professional_write",
  Update: "evo_med_people_professional_write",
  Delete: "evo_med_people_professional_write",
} as const;

export type EvoMedProfessionalPermissions =
  (typeof EvoMedProfessionalPermissions)[keyof typeof EvoMedProfessionalPermissions];

// Permissões para o paciente
export const EvoMedPatientPermissions = {
  List: "evo_med_people_patient_read",
  Get: "evo_med_people_patient_read",
  Create: "evo_med_people_patient_write",
  Update: "evo_med_people_patient_write",
  Delete: "evo_med_people_patient_write",
} as const;

export type EvoMedPatientPermissions =
  (typeof EvoMedPatientPermissions)[keyof typeof EvoMedPatientPermissions];

// ----- MedPeopleTypes

// Enum for EvoMed People Action - used in Professionals and Patients tracking
export type MedPeopleAction =
  | "CREATE_PROFESSIONAL"
  | "DELETE_PROFESSIONAL"
  | "UPDATE_PROFESSIONAL"
  | "UPDATE_PROFESSIONAL_PHOTO"
  | "UPDATE_PROFESSIONAL_PROFILE"
  | "UPDATE_PROFESSIONAL_PASSWORD"
  | "UPDATE_PROFESSIONAL_REPORT_TO"
  | "UPDATE_PROFESSIONAL_DEPARTMENT"
  | "UPDATE_PROFESSIONAL_OFFICE"
  | "UPDATE_PROFESSIONAL_COMPANY"
  | "UPDATE_PROFESSIONAL_JOB_TITLE"
  | "UPDATE_PROFESSIONAL_WORK_EMAIL"
  | "UPDATE_PROFESSIONAL_STATUS";

export enum IMedPeopleAction {
  Create_professional = "CREATE_PROFESSIONAL",
  Delete_professional = "DELETE_PROFESSIONAL",
  Update_professional = "UPDATE_PROFESSIONAL",
  Update_professional_photo = "UPDATE_PROFESSIONAL_PHOTO",
  Update_professional_profile = "UPDATE_PROFESSIONAL_PROFILE",
  Update_professional_password = "UPDATE_PROFESSIONAL_PASSWORD",
  Update_professional_report_to = "UPDATE_PROFESSIONAL_REPORT_TO",
  Update_professional_department = "UPDATE_PROFESSIONAL_DEPARTMENT",
  Update_professional_office = "UPDATE_PROFESSIONAL_OFFICE",
  Update_professional_company = "UPDATE_PROFESSIONAL_COMPANY",
  Update_professional_job_title = "UPDATE_PROFESSIONAL_JOB_TITLE",
  Update_professional_work_email = "UPDATE_PROFESSIONAL_WORK_EMAIL",
  Update_professional_status = "UPDATE_PROFESSIONAL_STATUS",
}

// export interface IProfessionalCounters {
//   active: number;
//   inactive: number;
//   draft: number;
//   deleted: number;
//   total: number;
// }

// Professional status
export type ProfessionalStatus = "active" | "inactive" | "draft";
export enum IProfessionalStatus {
  Active = "active",
  Inactive = "inactive",
  Draft = "draft",
}

export interface IProfessionalCouncil {
  doc: string;
  name: string;
  acronym: string;
  uf?: string | null;
  ref?: FirestoreDocumentReference;
}

export interface IProfessionalSpecialty {
  code: string;
  name: string;
  ref?: FirestoreDocumentReference;
}

export interface IProfessionalOffice {
  code: string;
  name: string;
  ref?: FirestoreDocumentReference;
}

export interface IProfessional extends IProfile {
  external_id?: string | null; // ID externo do médico
  status?: ProfessionalStatus;
  professionalId?: string | null;
  title?: string | null;
  type?: string | null;
  job_title?: string | null;
  councils?: IProfessionalCouncil[] | null;
  specialties?: IProfessionalSpecialty[] | null;
  offices?: IProfessionalOffice[] | null;
  date_joining?: Date | null;
  company_code?: string | null;
  company_name?: string | null;
  companyRef?: FirestoreDocumentReference;
  department_code?: string | null;
  department_name?: string | null;
  departmentRef?: FirestoreDocumentReference;
  address?: IAddress | null;
  contact?: IContact | null;
  tags?: ITag[] | null;
  userRef?: FirestoreDocumentReference;
  [key: string]: unknown; // index signature
}

// Patient status
export type PatientStatus = "active" | "inactive" | "draft" | "anonymized";
export enum IPatientStatus {
  Active = "active",
  Inactive = "inactive",
  Draft = "draft",
  Anonymized = "anonymized",
}

// Patient status
export type BloodType =
  | "A+"
  | "A-"
  | "B+"
  | "B-"
  | "AB+"
  | "AB-"
  | "O+"
  | "O-"
  | "Unknown";
export enum IBloodType {
  APositive = "A+",
  ANegative = "A-",
  BPositive = "B+",
  BNegative = "B-",
  ABPositive = "AB+",
  ABNegative = "AB-",
  OPositive = "O+",
  ONegative = "O-",
  Unknown = "Unknown",
}

export type Severity = "Mild" | "Moderate" | "Severe" | "Unknown";
export enum ISeverity {
  Mild = "Mild",
  Moderate = "Moderate",
  Severe = "Severe",
  Unknown = "Unknown",
}

export interface IPatientChronicCondition {
  code: string; // Identificador único da condição
  name: string; // Nome da doença crônica, ex: "Diabetes Tipo 2"
  ref?: FirestoreDocumentReference;
  diagnosis_date?: Date | null; // Data do diagnóstico (ISO 8601)
  severity?: Severity | null; // Nível de gravidade
  controlled?: boolean | null; // Indica se a condição está sob controle
  medications?: IMedication[] | null; // Lista de medicamentos utilizados para tratar a condição
  notes?: string | null; // Observações adicionais
}

export interface IPatientAllergy {
  code: string;
  name: string;
  kind?: "Food" | "Drug" | "Insect" | "Other" | null;
  ref?: FirestoreDocumentReference;
  severity?: Severity | null; // Nível de gravidade
}

export interface IPatientAttachment {
  id: string;
  filename: string;
  originalFilename: string;
  contentType: string;
  size: number;
  storagePath: string;
  category: string;
  baseCategory: PatientAttachmentBaseCategory;
  categoryColor?: string;
  externalLinks?: IExternalLink[];
  externalLinkKeys?: string[];
  uploadedBy?: { userId: string; displayName: string };
  uploadedAt: Date;
  updatedAt?: Date;
}

// Interface para contadores de appointments (usada em Patient e Calendar)
export interface IAppointmentCounters {
  statusBase?: Record<string, number>; // Contadores por statusBase (ex: scheduled, in_progress, cancelled, etc.)
  appointmentBaseType?: Record<string, number>; // Contadores por appointmentBaseType (ex: consultation, exam, procedure, etc.)
  total?: number; // Total de appointments não deletados
  deleted?: number; // Total de appointments deletados
}

export interface IPatient extends IProfile {
  external_id?: string | null; // ID externo do paciente
  social_id?: string | null; //CPF
  status?: PatientStatus;
  source_code?: string | null;
  source_name?: string | null;
  sourceRef?: FirestoreDocumentReference;
  address?: IAddress | null;
  contact?: IContact | null;
  lead_id?: string | null;
  chat_contact_id?: string | number | null;
  //informações médicas
  height_cm?: number | null; // Altura em centímetros
  weight_kg?: number | null; // Peso em kg
  blood_type?: BloodType | null; // Tipo sanguíneo
  allergies?: IPatientAllergy[] | null; // Lista de alergias conhecidas
  chronic_conditions?: IPatientChronicCondition[] | null; // Lista de doenças crônicas do paciente
  notes?: string | null; // Anotações sobre o paciente
  // Contadores de appointments
  appointmentCounters?: IAppointmentCounters | null;
  attachments?: IPatientAttachment[] | null;
  attachmentExternalLinkKeys?: string[] | null;
  tags?: ITag[] | null;
  userRef?: FirestoreDocumentReference;
  [key: string]: unknown; // index signature
}
