import uuid
from datetime import datetime
from typing import TYPE_CHECKING

from sqlalchemy import Boolean, DateTime, ForeignKey, String, Text, func  # noqa: F401
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.core.database import Base

if TYPE_CHECKING:
    from app.models.company import Company
    from app.models.user import User


class CaldavCompanyConfig(Base):
    """Zentraler Firmenkalender – alle genehmigten Abwesenheiten landen hier."""
    __tablename__ = "caldav_company_configs"

    id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    company_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("companies.id", ondelete="CASCADE"),
        nullable=False, unique=True, index=True,
    )
    enabled: Mapped[bool] = mapped_column(Boolean, default=False)
    principal_url: Mapped[str] = mapped_column(Text, nullable=False)
    calendar_url: Mapped[str | None] = mapped_column(Text)
    username: Mapped[str] = mapped_column(String(255), nullable=False)
    password_encrypted: Mapped[str] = mapped_column(Text, nullable=False)
    calendar_display_name: Mapped[str] = mapped_column(String(255), default="")
    verify_ssl: Mapped[bool] = mapped_column(Boolean, default=True)
    name_template: Mapped[str] = mapped_column(Text, default="$vorname $nachname – $typ")
    last_error: Mapped[str | None] = mapped_column(Text)
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
    )

    company: Mapped["Company"] = relationship("Company", lazy="noload")


class CaldavUserConfig(Base):
    """Persönlicher Kalender des Mitarbeiters."""
    __tablename__ = "caldav_user_configs"

    id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    user_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"),
        nullable=False, unique=True, index=True,
    )
    enabled: Mapped[bool] = mapped_column(Boolean, default=False)
    principal_url: Mapped[str] = mapped_column(Text, nullable=False)
    calendar_url: Mapped[str | None] = mapped_column(Text)
    username: Mapped[str] = mapped_column(String(255), nullable=False)
    password_encrypted: Mapped[str] = mapped_column(Text, nullable=False)
    calendar_display_name: Mapped[str] = mapped_column(String(255), default="")
    verify_ssl: Mapped[bool] = mapped_column(Boolean, default=True)
    last_error: Mapped[str | None] = mapped_column(Text)
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
    )

    user: Mapped["User"] = relationship("User", lazy="noload")
