import uuid
from sqlalchemy import Column, String, Date, Text, DateTime, ForeignKey, CheckConstraint
from sqlalchemy.orm import relationship
from utils.utils import Base, utc_now


class Patient(Base):
    __tablename__ = "patients"

    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    patient_number = Column(String(50), nullable=False, unique=True, index=True)
    first_name = Column(String(100), nullable=False)
    last_name = Column(String(100), nullable=False)
    date_of_birth = Column(Date, nullable=False)
    gender = Column(String(20), nullable=False)
    phone = Column(String(20), nullable=True)
    email = Column(String(100), nullable=True)
    address = Column(Text, nullable=True)
    city = Column(String(100), nullable=True)
    state = Column(String(100), nullable=True)
    postal_code = Column(String(20), nullable=True)
    country = Column(String(100), nullable=True)
    emergency_contact_name = Column(String(200), nullable=True)
    emergency_contact_phone = Column(String(20), nullable=True)
    emergency_contact_relationship = Column(String(100), nullable=True)
    insurance_provider_id = Column(String(36), ForeignKey("insurance_providers.id", ondelete="SET NULL"), nullable=True, index=True)
    insurance_policy_number = Column(String(100), nullable=True)
    created_at = Column(DateTime(timezone=True), default=utc_now, nullable=False)
    updated_at = Column(DateTime(timezone=True), default=utc_now, onupdate=utc_now, nullable=False)

    insurance_provider = relationship("Insuranceprovider", back_populates="patients")
    medical_record = relationship("MedicalRecord", back_populates="patient", uselist=False, cascade="all, delete-orphan", passive_deletes=True)
    appointments = relationship("Appointment", back_populates="patient", cascade="all, delete-orphan", passive_deletes=True)
    vital_signs = relationship("VitalSign", back_populates="patient", cascade="all, delete-orphan", passive_deletes=True)
    consultations = relationship("Consultation", back_populates="patient", cascade="all, delete-orphan", passive_deletes=True)
    prescriptions = relationship("Prescription", back_populates="patient", cascade="all, delete-orphan", passive_deletes=True)
    lab_tests = relationship("LabTest", back_populates="patient", cascade="all, delete-orphan", passive_deletes=True)
    invoices = relationship("Invoice", back_populates="patient", cascade="all, delete-orphan", passive_deletes=True)
    payments = relationship("Payment", back_populates="patient", cascade="all, delete-orphan", passive_deletes=True)

    __table_args__ = (
        CheckConstraint("gender IN ('male', 'female', 'other')", name="ck_patients_gender"),
    )


class MedicalRecord(Base):
    __tablename__ = "medical_records"

    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    patient_id = Column(String(36), ForeignKey("patients.id", ondelete="CASCADE"), nullable=False, unique=True, index=True)
    blood_type = Column(String(10), nullable=True)
    allergies = Column(Text, nullable=True)
    chronic_conditions = Column(Text, nullable=True)
    family_medical_history = Column(Text, nullable=True)
    surgical_history = Column(Text, nullable=True)
    immunization_history = Column(Text, nullable=True)
    notes = Column(Text, nullable=True)
    created_at = Column(DateTime(timezone=True), default=utc_now, nullable=False)
    updated_at = Column(DateTime(timezone=True), default=utc_now, onupdate=utc_now, nullable=False)

    patient = relationship("Patient", back_populates="medical_record")