from config import db
from datetime import datetime

class SubscriptionType(db.Model):
    __tablename__ = 'subscription_types'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    type_key = db.Column(db.String(50), unique=True, nullable=False)
    type_name = db.Column(db.String(255), nullable=False)
    description = db.Column(db.Text)
    category = db.Column(db.Enum('ai', 'education', 'premium', 'business', 'other'), default='other')
    icon = db.Column(db.String(255))
    color = db.Column(db.String(50))
    is_active = db.Column(db.Boolean, default=True)
    display_order = db.Column(db.Integer, default=0)
    metadata_obj = db.Column('metadata', db.JSON)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    def to_dict(self):
        return {
            'id': self.id,
            'type_key': self.type_key,
            'type_name': self.type_name,
            'description': self.description,
            'category': self.category,
            'icon': self.icon,
            'color': self.color,
            'is_active': self.is_active,
            'display_order': self.display_order
        }

class SubscriptionPlan(db.Model):
    __tablename__ = 'subscription_plans'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    subscription_type_id = db.Column(db.Integer, db.ForeignKey('subscription_types.id'), nullable=False)
    plan_name = db.Column(db.String(255), nullable=False)
    plan_slug = db.Column(db.String(100), unique=True, nullable=False)
    tier = db.Column(db.Enum('free', 'basic', 'premium', 'enterprise'), nullable=False)
    description = db.Column(db.Text)
    price_monthly_npr = db.Column(db.Numeric(10, 2), default=0.00)
    price_yearly_npr = db.Column(db.Numeric(10, 2))
    price_monthly_usd = db.Column(db.Numeric(10, 2))
    price_yearly_usd = db.Column(db.Numeric(10, 2))
    discount_yearly_percent = db.Column(db.Numeric(5, 2), default=0.00)
    benefits = db.Column(db.JSON, nullable=False)
    features_list = db.Column(db.JSON)
    is_popular = db.Column(db.Boolean, default=False)
    badge_text = db.Column(db.String(50))
    is_active = db.Column(db.Boolean, default=True)
    display_order = db.Column(db.Integer, default=0)
    max_users_per_subscription = db.Column(db.Integer, default=1)
    trial_days = db.Column(db.Integer, default=0)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    subscription_type = db.relationship('SubscriptionType', backref='plans')

    def to_dict(self):
        return {
            'id': self.id,
            'subscription_type_id': self.subscription_type_id,
            'subscription_type_name': self.subscription_type.type_name if self.subscription_type else None,
            'plan_name': self.plan_name,
            'plan_slug': self.plan_slug,
            'tier': self.tier,
            'description': self.description,
            'price_monthly_npr': float(self.price_monthly_npr) if self.price_monthly_npr else 0,
            'price_yearly_npr': float(self.price_yearly_npr) if self.price_yearly_npr else 0,
            'discount_yearly_percent': float(self.discount_yearly_percent) if self.discount_yearly_percent else 0,
            'benefits': self.benefits,
            'features_list': self.features_list,
            'is_popular': self.is_popular,
            'badge_text': self.badge_text,
            'is_active': self.is_active,
            'display_order': self.display_order,
            'trial_days': self.trial_days
        }

class UserSubscription(db.Model):
    __tablename__ = 'user_subscriptions'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, nullable=False)
    plan_id = db.Column(db.Integer, db.ForeignKey('subscription_plans.id'), nullable=False)
    subscription_type_id = db.Column(db.Integer, db.ForeignKey('subscription_types.id'), nullable=False)
    status = db.Column(db.Enum('active', 'trial', 'cancelled', 'expired', 'paused', 'pending'), default='active')
    billing_cycle = db.Column(db.Enum('monthly', 'yearly', 'lifetime'), default='monthly')
    started_at = db.Column(db.DateTime, default=datetime.utcnow)
    trial_ends_at = db.Column(db.DateTime)
    current_period_start = db.Column(db.DateTime, nullable=False)
    current_period_end = db.Column(db.DateTime, nullable=False)
    expires_at = db.Column(db.DateTime, nullable=False)
    next_billing_date = db.Column(db.Date)
    auto_renew = db.Column(db.Boolean, default=True)
    payment_method = db.Column(db.String(50))
    last_payment_id = db.Column(db.String(255))
    last_payment_amount = db.Column(db.Numeric(10, 2))
    last_payment_date = db.Column(db.DateTime)
    cancel_at_period_end = db.Column(db.Boolean, default=False)
    cancelled_at = db.Column(db.DateTime)
    cancellation_reason = db.Column(db.Text)
    cancelled_by = db.Column(db.Enum('user', 'admin', 'payment_failed', 'system'))
    usage_data = db.Column(db.JSON)
    metadata_obj = db.Column('metadata', db.JSON)
    notes = db.Column(db.Text)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
