from sqlalchemy import Column, Integer, String, Boolean, DateTime, Float, ForeignKey, Enum as SQLEnum
from sqlalchemy.orm import Mapped, relationship
from sqlalchemy.sql import func
from typing import TYPE_CHECKING, Optional
from datetime import datetime

from app.database.database import Base
import enum


class SubscriptionPlanType(str, enum.Enum):
    FREE = "FREE"
    PRO = "PRO"


class SubscriptionStatus(str, enum.Enum):
    ACTIVE = "ACTIVE"
    EXPIRED = "EXPIRED"
    CANCELLED = "CANCELLED"


class SubscriptionPlan(Base):
    """Subscription plan model (Free and Pro)"""
    __tablename__ = "subscription_plans"
    
    plan_id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    plan_type = Column(SQLEnum(SubscriptionPlanType), nullable=False, unique=True, index=True)
    name = Column(String(100), nullable=False)  # "Free" or "Pro"
    price = Column(Float, nullable=False)  # 0.0 for Free, 14.99 for Pro
    price_id = Column(String(255), nullable=True)  # Stripe Price ID for Pro plan
    billing_period = Column(String(50), nullable=False, default="month")  # "month" or "year"
    is_active = Column(Boolean, default=True, nullable=False)
    created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False)
    
    if TYPE_CHECKING:
        from app.models.user.subscription_model import UserSubscription
    
    subscriptions: Mapped[list["UserSubscription"]] = relationship("UserSubscription", back_populates="plan")


class UserSubscription(Base):
    """User subscription model to track user subscriptions"""
    __tablename__ = "user_subscriptions"
    
    subscription_id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey("users.user_id", ondelete="CASCADE"), nullable=False, index=True)
    plan_id = Column(Integer, ForeignKey("subscription_plans.plan_id"), nullable=False)
    status = Column(SQLEnum(SubscriptionStatus), nullable=False, default=SubscriptionStatus.ACTIVE)
    
    # Stripe fields
    stripe_subscription_id = Column(String(255), nullable=True, unique=True, index=True)  # Stripe subscription ID
    stripe_customer_id = Column(String(255), nullable=True, index=True)  # Stripe customer ID
    stripe_payment_intent_id = Column(String(255), nullable=True)  # For initial payment
    
    # Subscription dates
    started_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
    expires_at = Column(DateTime(timezone=True), nullable=True)  # Null for active subscriptions
    cancelled_at = Column(DateTime(timezone=True), nullable=True)
    
    # Auto-renewal
    auto_renew = Column(Boolean, default=True, nullable=False)
    
    created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False)
    
    if TYPE_CHECKING:
        from app.models.user.user_model import User
    
    user: Mapped["User"] = relationship("User", back_populates="subscriptions")
    plan: Mapped["SubscriptionPlan"] = relationship("SubscriptionPlan", back_populates="subscriptions")
