"""Seed Model Pricing Data"""
import sys
import os

# Add parent dir to path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app.database import SessionLocal
from app.models.pricing import ModelPricing
from app.database import engine, Base

def seed_pricing():
    # Create tables if they don't exist
    Base.metadata.create_all(bind=engine)
    
    db = SessionLocal()
    
    # Pricing Data (from model_costs.md)
    pricing_data = [
        # 1. Text & Vision
        {
            "provider": "openai", "model_name": "gpt-4o",
            "cost_per_input_token": 0.0000025,  # $2.50 / 1M
            "cost_per_output_token": 0.0000100  # $10.00 / 1M
        },
        {
            "provider": "openai", "model_name": "gpt-4o-mini",
            "cost_per_input_token": 0.00000015, # $0.15 / 1M
            "cost_per_output_token": 0.00000060 # $0.60 / 1M
        },
        {
            "provider": "google", "model_name": "gemini-2.0-flash-exp",
            "cost_per_request": 0.0 # Currently free/preview
        },
        
        # 2. Image Generation
        {
            "provider": "openai", "model_name": "dall-e-3",
            "cost_per_image": 0.080 # Standard HD avg
        },
        {
            "provider": "openai", "model_name": "gpt-image-1",
            "cost_per_image": 0.040 
        },
        {
            "provider": "google", "model_name": "imagen-3.0",
            "cost_per_image": 0.040
        },
        {
            "provider": "nano-banana", "model_name": "gemini-2.5-flash-image",
            "cost_per_image": 0.040
        },
        {
            "provider": "stability", "model_name": "sd3.5-large",
            "cost_per_image": 0.065
        },
        {
            "provider": "ideogram", "model_name": "V_2",
            "cost_per_image": 0.080
        },
        {
            "provider": "flux", "model_name": "flux-pro-1.1",
            "cost_per_image": 0.050
        },
        
        # 3. Video Generation
        {
            "provider": "runway", "model_name": "gen3_alpha",
            "cost_per_second": 0.10
        },
        {
            "provider": "runway", "model_name": "gen3_alpha_turbo",
            "cost_per_second": 0.05
        },
        {
            "provider": "runway", "model_name": "gen4_turbo",
            "cost_per_second": 0.15 # Estimate
        },
        {
            "provider": "google", "model_name": "veo-3.1",
            "cost_per_second": 0.15 
        },
        {
            "provider": "veo", "model_name": "veo-3.1-generate-preview", # Provider 'veo' or 'google'? Log said 'veo'
            "cost_per_second": 0.15 
        },
        {
             "provider": "google", "model_name": "veo-3.1-generate-preview", # Alias for safety
             "cost_per_second": 0.15
        },
        {
            "provider": "google", "model_name": "veo-3",
            "cost_per_second": 0.10
        },
        
        # 3b. Misc Image
        {
            "provider": "nano-banana", "model_name": "gemini-3-pro-image-preview",
            "cost_per_image": 0.040
        },

        # 4. Audio
        {
            "provider": "elevenlabs", "model_name": "eleven_multilingual_v2",
            "cost_per_1k_chars": 0.30
        },
        {
            "provider": "elevenlabs", "model_name": "eleven_flash_v2_5",
            "cost_per_1k_chars": 0.15
        },
        {
            "provider": "elevenlabs", "model_name": "sound-generation",
            "cost_per_request": 0.10 # Per generation estimate
        },
        {
            "provider": "deepl", "model_name": "api-pro",
            "cost_per_1k_chars": 0.025 # $25 per 1M
        },
        
        # 5. Media Editing
        {
            "provider": "clipping_magic", "model_name": "api-v1",
            "cost_per_image": 0.10
        },
        {
            "provider": "topaz", "model_name": "image-v1",
            "cost_per_image": 0.20 # Buffer estimate
        },
        {
            "provider": "topaz", "model_name": "video-v1",
            "cost_per_second": 0.30 # Buffer estimate
        }
    ]
    
    print(f"Seeding {len(pricing_data)} pricing records...")
    
    try:
        # Clear existing
        db.query(ModelPricing).delete()
        
        for data in pricing_data:
            pricing = ModelPricing(**data)
            db.add(pricing)
        
        db.commit()
        print("✓ Pricing data seeded successfully.")
        
    except Exception as e:
        print(f"Error seeding data: {e}")
        db.rollback()
    finally:
        db.close()

if __name__ == "__main__":
    seed_pricing()
