from datetime import datetime, timezone

from app.extensions import db


class Calendar(db.Model):
    __tablename__ = 'calendars'

    id = db.Column(db.Integer, primary_key=True)
    owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False, index=True)
    name = db.Column(db.String(255), nullable=False)
    color = db.Column(db.String(7), default='#3788d8')
    description = db.Column(db.Text, nullable=True)
    ical_token = db.Column(db.String(64), unique=True, nullable=True, index=True)
    created_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc))
    updated_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc),
                           onupdate=lambda: datetime.now(timezone.utc))

    events = db.relationship('CalendarEvent', backref='calendar', lazy='dynamic',
                             cascade='all, delete-orphan')
    shares = db.relationship('CalendarShare', backref='calendar', lazy='dynamic',
                             cascade='all, delete-orphan')

    def to_dict(self):
        return {
            'id': self.id,
            'owner_id': self.owner_id,
            'name': self.name,
            'color': self.color,
            'description': self.description,
            'ical_token': self.ical_token,
            'created_at': self.created_at.isoformat() if self.created_at else None,
        }


class CalendarEvent(db.Model):
    __tablename__ = 'calendar_events'

    id = db.Column(db.Integer, primary_key=True)
    calendar_id = db.Column(db.Integer, db.ForeignKey('calendars.id'), nullable=False, index=True)
    uid = db.Column(db.String(255), unique=True, nullable=False)
    ical_data = db.Column(db.Text, nullable=False)  # Full VCALENDAR component
    summary = db.Column(db.String(500), nullable=True)
    dtstart = db.Column(db.DateTime, nullable=True, index=True)
    dtend = db.Column(db.DateTime, nullable=True)
    all_day = db.Column(db.Boolean, default=False)
    recurrence_rule = db.Column(db.Text, nullable=True)
    created_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc))
    updated_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc),
                           onupdate=lambda: datetime.now(timezone.utc))

    def to_dict(self):
        return {
            'id': self.id,
            'calendar_id': self.calendar_id,
            'uid': self.uid,
            'summary': self.summary,
            'dtstart': self.dtstart.isoformat() if self.dtstart else None,
            'dtend': self.dtend.isoformat() if self.dtend else None,
            'all_day': self.all_day,
            'recurrence_rule': self.recurrence_rule,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None,
        }


class CalendarShare(db.Model):
    __tablename__ = 'calendar_shares'

    id = db.Column(db.Integer, primary_key=True)
    calendar_id = db.Column(db.Integer, db.ForeignKey('calendars.id'), nullable=False, index=True)
    shared_with_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False, index=True)
    permission = db.Column(db.String(20), nullable=False, default='read')  # 'read' or 'readwrite'

    shared_with = db.relationship('User', backref='shared_calendars')

    __table_args__ = (
        db.UniqueConstraint('calendar_id', 'shared_with_id', name='uq_calendar_share'),
    )
