import uuid

from sqlalchemy import Boolean, Column, Date, Integer, String, Text
from sqlalchemy.dialects.postgresql import UUID

from shared.database import BaseModel


class CalendarEntry(BaseModel):
    __tablename__ = "calendar_entries"

    uuid = Column(UUID(as_uuid=True), default=uuid.uuid4, unique=True, index=True)
    user_id = Column(Integer, nullable=False, index=True)  # Убран ForeignKey

    entry_date = Column(Date, nullable=False, index=True)
    entry_type = Column(
        String(50), nullable=False
    )  # period, ovulation, symptoms, medication, etc.

    # Period tracking
    flow_intensity = Column(String(20))  # light, medium, heavy
    period_symptoms = Column(Text)  # cramps, headache, mood, etc.

    # General health
    mood = Column(String(20))  # happy, sad, anxious, irritated, etc.
    energy_level = Column(Integer)  # 1-5 scale
    sleep_hours = Column(Integer)

    # Symptoms and notes
    symptoms = Column(Text)  # Any symptoms experienced
    medications = Column(Text)  # Medications taken
    notes = Column(Text)  # Personal notes

    # Predictions and calculations
    is_predicted = Column(Boolean, default=False)  # If this is a predicted entry
    confidence_score = Column(Integer)  # Prediction confidence 1-100

    def __repr__(self):
        return f"<CalendarEntry user_id={self.user_id} date={self.entry_date} type={self.entry_type}>"


class CycleData(BaseModel):
    __tablename__ = "cycle_data"

    user_id = Column(Integer, nullable=False, index=True)  # Убран ForeignKey
    cycle_start_date = Column(Date, nullable=False)
    cycle_length = Column(Integer)  # Length of this cycle
    period_length = Column(Integer)  # Length of period in this cycle

    # Calculated fields
    ovulation_date = Column(Date)
    fertile_window_start = Column(Date)
    fertile_window_end = Column(Date)
    next_period_predicted = Column(Date)

    # Cycle characteristics
    cycle_regularity_score = Column(Integer)  # 1-100, how regular is this cycle
    avg_cycle_length = Column(Integer)  # Rolling average
    avg_period_length = Column(Integer)  # Rolling average

    def __repr__(self):
        return f"<CycleData user_id={self.user_id} start={self.cycle_start_date}>"


class HealthInsights(BaseModel):
    __tablename__ = "health_insights"

    user_id = Column(Integer, nullable=False, index=True)  # Убран ForeignKey
    insight_type = Column(
        String(50), nullable=False
    )  # cycle_pattern, symptom_pattern, etc.

    title = Column(String(200), nullable=False)
    description = Column(Text, nullable=False)
    recommendation = Column(Text)

    # Metadata
    confidence_level = Column(String(20))  # high, medium, low
    data_points_used = Column(Integer)  # How many data points were used
    is_dismissed = Column(Boolean, default=False)

    def __repr__(self):
        return f"<HealthInsights user_id={self.user_id} type={self.insight_type}>"
