import uuid
from datetime import datetime

from sqlalchemy import String, Integer, Text, DateTime, ForeignKey, Index, func
from sqlalchemy import Uuid
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.database import Base, JSONType


class GeoPlan(Base):
    __tablename__ = "geo_plans"

    id: Mapped[uuid.UUID] = mapped_column(
        Uuid(as_uuid=True),
        primary_key=True,
        default=uuid.uuid4,
    )
    organization_id: Mapped[uuid.UUID] = mapped_column(
        Uuid(as_uuid=True),
        ForeignKey("organizations.id", ondelete="CASCADE"),
        nullable=False,
    )
    brand_id: Mapped[uuid.UUID] = mapped_column(
        Uuid(as_uuid=True),
        ForeignKey("brands.id", ondelete="CASCADE"),
        nullable=False,
    )
    title: Mapped[str] = mapped_column(String(500), nullable=False)
    status: Mapped[str] = mapped_column(
        String(20), server_default="draft", nullable=False,
    )
    diagnosis_score: Mapped[int] = mapped_column(Integer, nullable=False)
    target_score: Mapped[int] = mapped_column(Integer, nullable=False)
    estimated_weeks: Mapped[int] = mapped_column(Integer, nullable=False)
    plan_data: Mapped[dict | None] = mapped_column(JSONType, nullable=True)
    source: Mapped[str] = mapped_column(String(20), nullable=False, default="rule")
    created_by: Mapped[str | None] = mapped_column(
        String(36),
        ForeignKey("users.id", ondelete="SET NULL"),
        nullable=True,
    )
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        server_default=func.now(),
        nullable=False,
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        server_default=func.now(),
        onupdate=func.now(),
        nullable=False,
    )

    brand: Mapped["Brand"] = relationship("Brand")
    creator: Mapped["User | None"] = relationship(
        "User", foreign_keys=[created_by]
    )

    __table_args__ = (
        Index("idx_geo_plans_brand_id", "brand_id"),
        Index("idx_geo_plans_status", "status"),
        Index("idx_geo_plans_organization_id", "organization_id"),
    )


class GeoPlanAction(Base):
    __tablename__ = "geo_plan_actions"

    id: Mapped[uuid.UUID] = mapped_column(
        Uuid(as_uuid=True),
        primary_key=True,
        default=uuid.uuid4,
    )
    plan_id: Mapped[uuid.UUID] = mapped_column(
        Uuid(as_uuid=True),
        ForeignKey("geo_plans.id", ondelete="CASCADE"),
        nullable=False,
    )
    action_type: Mapped[str] = mapped_column(String(50), nullable=False)
    title: Mapped[str] = mapped_column(String(500), nullable=False)
    description: Mapped[str] = mapped_column(Text, nullable=False)
    reason: Mapped[str] = mapped_column(Text, nullable=False)
    priority: Mapped[str] = mapped_column(String(10), nullable=False)
    status: Mapped[str] = mapped_column(
        String(20), server_default="pending", nullable=False,
    )
    target_keyword: Mapped[str | None] = mapped_column(String(200), nullable=True)
    target_platform: Mapped[str | None] = mapped_column(String(50), nullable=True)
    content_style: Mapped[str | None] = mapped_column(String(50), nullable=True)
    estimated_impact: Mapped[str | None] = mapped_column(String(500), nullable=True)
    difficulty: Mapped[str] = mapped_column(String(10), nullable=False)
    execution_params: Mapped[dict | None] = mapped_column(JSONType, nullable=True)
    sort_order: Mapped[int] = mapped_column(
        Integer, server_default="0", nullable=False,
    )
    completed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        server_default=func.now(),
        nullable=False,
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        server_default=func.now(),
        onupdate=func.now(),
        nullable=False,
    )

    plan: Mapped["GeoPlan"] = relationship("GeoPlan")

    __table_args__ = (
        Index("idx_geo_plan_actions_plan_id", "plan_id"),
        Index("idx_geo_plan_actions_status", "status"),
        Index("idx_geo_plan_actions_priority", "priority"),
    )
