export type UserRole = "owner" | "admin" | "doctor" | "receptionist";
export type AppointmentStatus = "scheduled" | "confirmed" | "in_progress" | "completed" | "cancelled" | "no_show";
export type AppointmentType = "primera_vez" | "seguimiento" | "urgencia";
export type CfdiStatus = "draft" | "stamped" | "cancelled";
export type PaymentStatus = "pending" | "partial" | "paid";
export type PaymentMethod = "cash" | "card" | "transfer" | "other";
export type InventoryMovementType = "entrada" | "salida" | "ajuste" | "merma";
export type ProductCategory = "medicamento" | "insumo" | "material" | "equipo";
export type SubscriptionPlan = "basico" | "pro" | "enterprise" | "trial";
export type SubscriptionStatus = "active" | "trial" | "suspended" | "cancelled";

export interface Clinic {
  id: string;
  name: string;
  slug: string;
  logo_url: string | null;
  address: string | null;
  phone: string | null;
  email: string | null;
  rfc: string | null;
  razon_social: string | null;
  regimen_fiscal: string | null;
  codigo_postal: string | null;
  pac_provider: string | null;
  pac_api_key: string | null;
  subscription_plan: SubscriptionPlan;
  subscription_status: SubscriptionStatus;
  trial_ends_at: string | null;
  created_at: string;
}

export interface User {
  id: string;
  clinic_id: string;
  full_name: string;
  email: string;
  phone: string | null;
  role: UserRole;
  specialty: string | null;
  license_number: string | null;
  color: string | null;
  is_active: boolean;
  avatar_url: string | null;
  created_at: string;
}

export interface Address {
  calle: string;
  colonia: string;
  cp: string;
  ciudad: string;
  estado: string;
}

export interface EmergencyContact {
  nombre: string;
  telefono: string;
  parentesco: string;
}

export interface Patient {
  id: string;
  clinic_id: string;
  first_name: string;
  last_name: string;
  date_of_birth: string | null;
  gender: "M" | "F" | "otro" | null;
  curp: string | null;
  phone: string | null;
  email: string | null;
  address: Address | null;
  blood_type: string | null;
  allergies: string[];
  emergency_contact: EmergencyContact | null;
  notes: string | null;
  source: string | null;
  is_active: boolean;
  created_at: string;
}

export interface DoctorSchedule {
  id: string;
  clinic_id: string;
  doctor_id: string;
  day_of_week: number;
  start_time: string;
  end_time: string;
  slot_duration: number;
  is_active: boolean;
}

export interface Appointment {
  id: string;
  clinic_id: string;
  patient_id: string;
  doctor_id: string;
  starts_at: string;
  ends_at: string;
  status: AppointmentStatus;
  type: AppointmentType;
  reason: string | null;
  notes: string | null;
  reminder_sent: boolean;
  created_by: string;
  created_at: string;
  patient?: Patient;
  doctor?: User;
}

export interface VitalSigns {
  peso?: number;
  talla?: number;
  ta_sistolica?: number;
  ta_diastolica?: number;
  fc?: number;
  fr?: number;
  temp?: number;
  spo2?: number;
}

export interface Diagnosis {
  code: string;
  description: string;
  type: "principal" | "secundario";
}

export interface MedicalRecord {
  id: string;
  clinic_id: string;
  patient_id: string;
  family_history: { enfermedad: string; parentesco: string }[];
  personal_history: { enfermedad: string; anio_diagnostico: string; tratamiento: string }[];
  surgical_history: { cirugia: string; fecha: string; notas: string }[];
  current_medications: { medicamento: string; dosis: string; frecuencia: string }[];
  immunizations: { vacuna: string; fecha: string }[];
  created_at: string;
  updated_at: string;
}

export interface ConsultationNote {
  id: string;
  clinic_id: string;
  patient_id: string;
  doctor_id: string;
  appointment_id: string | null;
  subjective: string | null;
  objective: string | null;
  assessment: string | null;
  plan: string | null;
  vital_signs: VitalSigns | null;
  diagnoses: Diagnosis[];
  is_signed: boolean;
  signed_at: string | null;
  created_at: string;
}

export interface PrescriptionItem {
  medicamento: string;
  dosis: string;
  via: string;
  frecuencia: string;
  duracion: string;
  indicaciones: string;
}

export interface Prescription {
  id: string;
  clinic_id: string;
  consultation_id: string;
  patient_id: string;
  doctor_id: string;
  items: PrescriptionItem[];
  pharmacy_notes: string | null;
  pdf_url: string | null;
  created_at: string;
}

export interface ServiceCatalog {
  id: string;
  clinic_id: string;
  name: string;
  description: string | null;
  price: number;
  sat_product_key: string | null;
  sat_unit_key: string | null;
  tax_rate: number;
  is_active: boolean;
  created_at: string;
}

export interface Invoice {
  id: string;
  clinic_id: string;
  patient_id: string;
  invoice_number: string;
  cfdi_uuid: string | null;
  cfdi_status: CfdiStatus;
  cfdi_xml_url: string | null;
  cfdi_pdf_url: string | null;
  uso_cfdi: string | null;
  forma_pago: string | null;
  metodo_pago: string | null;
  subtotal: number;
  tax_amount: number;
  total: number;
  payment_status: PaymentStatus;
  paid_amount: number;
  due_date: string | null;
  created_by: string;
  created_at: string;
  patient?: Patient;
  items?: InvoiceItem[];
}

export interface InvoiceItem {
  id: string;
  invoice_id: string;
  service_id: string | null;
  product_id: string | null;
  description: string;
  quantity: number;
  unit_price: number;
  tax_rate: number;
  total: number;
}

export interface Payment {
  id: string;
  clinic_id: string;
  invoice_id: string;
  amount: number;
  payment_method: PaymentMethod;
  reference: string | null;
  received_by: string;
  created_at: string;
}

export interface Product {
  id: string;
  clinic_id: string;
  sku: string | null;
  name: string;
  description: string | null;
  category: ProductCategory;
  unit: string;
  purchase_price: number;
  sale_price: number;
  min_stock: number;
  is_active: boolean;
  created_at: string;
}

export interface InventoryStock {
  id: string;
  clinic_id: string;
  product_id: string;
  current_stock: number;
  last_updated: string;
}

export interface InventoryMovement {
  id: string;
  clinic_id: string;
  product_id: string;
  type: InventoryMovementType;
  quantity: number;
  reason: string | null;
  reference_id: string | null;
  created_by: string;
  created_at: string;
}

export interface Branch {
  id: string;
  clinic_id: string;
  name: string;
  address: string | null;
  phone: string | null;
  is_main: boolean;
  is_active: boolean;
}
