from config import db
from datetime import datetime

class AIModel(db.Model):
    __tablename__ = 'ai_models'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(255), unique=True, nullable=False)
    description = db.Column(db.Text)
    tier = db.Column(db.Enum('free', 'paid', 'premium'), default='free')
    model = db.Column(db.String(255), nullable=False)
    provider = db.Column(db.String(100), nullable=False)
    api_key = db.Column(db.Text)
    base_url = db.Column(db.String(500))
    status = db.Column(db.Enum('active', 'inactive', 'working', 'deprecated'), default='active')
    capabilities = db.Column(db.JSON)
    max_tokens = db.Column(db.Integer, default=8192)
    auth_required = db.Column(db.Boolean, default=False)
    is_active = db.Column(db.Boolean, default=True)
    cost_per_1k_input_tokens = db.Column(db.Numeric(10, 4), default=0.5000)
    cost_per_1k_output_tokens = db.Column(db.Numeric(10, 4), default=1.0000)
    is_free = db.Column(db.Boolean, default=False)
    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,
            'name': self.name,
            'description': self.description,
            'tier': self.tier,
            'model': self.model,
            'provider': self.provider,
            'base_url': self.base_url,
            'status': self.status,
            'capabilities': self.capabilities,
            'max_tokens': self.max_tokens,
            'auth_required': self.auth_required,
            'is_active': self.is_active,
            'cost_per_1k_input_tokens': float(self.cost_per_1k_input_tokens) if self.cost_per_1k_input_tokens else 0,
            'cost_per_1k_output_tokens': float(self.cost_per_1k_output_tokens) if self.cost_per_1k_output_tokens else 0,
            'is_free': self.is_free,
            'created_at': self.created_at.isoformat() if self.created_at else None
        }

class AITokenPackage(db.Model):
    __tablename__ = 'ai_token_packages'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    package_name = db.Column(db.String(100), nullable=False)
    description = db.Column(db.Text)
    token_amount = db.Column(db.Numeric(10, 2), nullable=False)
    bonus_tokens = db.Column(db.Numeric(10, 2), default=0.00)
    price_npr = db.Column(db.Numeric(10, 2), nullable=False)
    price_usd = db.Column(db.Numeric(10, 2), nullable=False)
    discount_percentage = db.Column(db.Integer, default=0)
    is_popular = db.Column(db.Boolean, default=False)
    is_active = db.Column(db.Boolean, default=True)
    validity_days = db.Column(db.Integer)
    features = db.Column(db.Text)
    display_order = 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)

    def to_dict(self):
        return {
            'id': self.id,
            'package_name': self.package_name,
            'description': self.description,
            'token_amount': float(self.token_amount) if self.token_amount else 0,
            'bonus_tokens': float(self.bonus_tokens) if self.bonus_tokens else 0,
            'price_npr': float(self.price_npr) if self.price_npr else 0,
            'price_usd': float(self.price_usd) if self.price_usd else 0,
            'discount_percentage': self.discount_percentage,
            'is_popular': self.is_popular,
            'is_active': self.is_active,
            'validity_days': self.validity_days,
            'features': self.features,
            'display_order': self.display_order
        }

class AITokenTransaction(db.Model):
    __tablename__ = 'ai_token_transactions'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, nullable=False)
    transaction_type = db.Column(db.Enum('purchase', 'consumption', 'bonus', 'refund', 'subscription'), nullable=False)
    amount = db.Column(db.Numeric(10, 2), nullable=False)
    balance_before = db.Column(db.Numeric(10, 2), nullable=False)
    balance_after = db.Column(db.Numeric(10, 2), nullable=False)
    package_id = db.Column(db.Integer)
    description = db.Column(db.Text)
    metadata_json = db.Column('metadata', db.JSON)
    payment_method = db.Column(db.String(50))
    payment_reference = db.Column(db.String(255))
    status = db.Column(db.Enum('pending', 'completed', 'failed', 'refunded'), default='completed')
    ai_model = db.Column(db.String(255))
    endpoint = db.Column(db.String(255))
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
