from sqlalchemy import Column, String, Integer, Boolean, Date, Time, DateTime, ForeignKey, Text
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.sql import func
import uuid

from app.core.database import Base

class Habit(Base):
    __tablename__ = "habits"
    
    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    user_id = Column(UUID(as_uuid=True), ForeignKey('users.id', ondelete='CASCADE'), nullable=False, index=True)
    name = Column(String(255), nullable=False)
    description = Column(Text)
    frequency_type = Column(String(50), default='daily')
    target_count = Column(Integer, default=1)
    reminder_time = Column(Time)
    start_date = Column(Date, default=func.current_date())
    end_date = Column(Date)
    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

class HabitCompletion(Base):
    __tablename__ = "habit_completions"
    
    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    habit_id = Column(UUID(as_uuid=True), ForeignKey('habits.id', ondelete='CASCADE'), nullable=False, index=True)
    user_id = Column(UUID(as_uuid=True), ForeignKey('users.id', ondelete='CASCADE'), nullable=False, index=True)
    completion_date = Column(Date, nullable=False, index=True)
    completion_time = Column(Time)
    notes = Column(Text)
    quality_rating = Column(Integer)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
