"""
Seed data for the e-commerce database.
Run this script with: python seed.py
"""

from sqlalchemy.orm import Session
from app.database.database import SessionLocal, engine, Base
from app.models import Category, Product, User
from app.services.auth import get_password_hash
import json

# Create tables
Base.metadata.create_all(bind=engine)


def seed_database():
    db = SessionLocal()

    # Check if data already exists
    if db.query(Category).first():
        print("Database already seeded!")
        db.close()
        return

    # Create categories
    categories = [
        Category(name="Shoes", slug="shoes", description="Footwear for all occasions"),
        Category(name="Shirts", slug="shirts", description="Men's and women's shirts"),
        Category(name="Pants", slug="pants", description="Trousers and jeans"),
        Category(name="Hats", slug="hats", description="Caps, beanies, and more"),
        Category(name="Accessories", slug="accessories", description="Belts, scarves, and more"),
    ]

    db.add_all(categories)
    db.flush()

    # Create sample products (focus on shoes)
    products_data = [
        # Shoes
        {
            "name": "Premium Running Shoes",
            "description": "High-performance running shoes with advanced cushioning technology",
            "price": 129.99,
            "discount_price": 89.99,
            "category_id": 1,
            "gender": "men",
            "brand": "Nike",
            "sizes": ["6", "7", "8", "9", "10", "11", "12", "13"],
            "colors": ["Black", "White", "Blue", "Red"],
            "stock": 50,
            "images": ["https://via.placeholder.com/500x500?text=Running+Shoes"],
            "is_featured": True,
            "is_on_sale": True,
        },
        {
            "name": "Women's Athletic Sneakers",
            "description": "Comfortable and stylish sneakers for daily wear and workout",
            "price": 119.99,
            "discount_price": None,
            "category_id": 1,
            "gender": "women",
            "brand": "Adidas",
            "sizes": ["5", "6", "7", "8", "9", "10", "11"],
            "colors": ["Pink", "Purple", "White", "Black"],
            "stock": 35,
            "images": ["https://via.placeholder.com/500x500?text=Womens+Sneakers"],
            "is_featured": True,
            "is_on_sale": False,
        },
        {
            "name": "Casual Leather Loafers",
            "description": "Elegant leather loafers perfect for office or casual outings",
            "price": 159.99,
            "discount_price": 129.99,
            "category_id": 1,
            "gender": "men",
            "brand": "Cole Haan",
            "sizes": ["7", "8", "9", "10", "11", "12", "13"],
            "colors": ["Brown", "Black", "Tan"],
            "stock": 25,
            "images": ["https://via.placeholder.com/500x500?text=Loafers"],
            "is_featured": True,
            "is_on_sale": True,
        },
        {
            "name": "Summer Flip Flops",
            "description": "Comfortable flip flops for beach and casual summer wear",
            "price": 29.99,
            "discount_price": 19.99,
            "category_id": 1,
            "gender": "women",
            "brand": "Havaianas",
            "sizes": ["6", "7", "8", "9", "10"],
            "colors": ["Turquoise", "Pink", "Yellow", "White"],
            "stock": 100,
            "images": ["https://via.placeholder.com/500x500?text=Flip+Flops"],
            "is_featured": False,
            "is_on_sale": True,
        },
        {
            "name": "Basketball High Tops",
            "description": "Professional basketball shoes with superior ankle support",
            "price": 179.99,
            "discount_price": 149.99,
            "category_id": 1,
            "gender": "men",
            "brand": "Jordan",
            "sizes": ["7", "8", "9", "10", "11", "12", "13", "14"],
            "colors": ["Black", "Red", "White", "Gold"],
            "stock": 40,
            "images": ["https://via.placeholder.com/500x500?text=Basketball+Shoes"],
            "is_featured": True,
            "is_on_sale": False,
        },
        {
            "name": "Hiking Boot Pro",
            "description": "Durable hiking boots with waterproof protection and excellent grip",
            "price": 189.99,
            "discount_price": 149.99,
            "category_id": 1,
            "gender": "men",
            "brand": "Salomon",
            "sizes": ["6", "7", "8", "9", "10", "11", "12", "13"],
            "colors": ["Brown", "Gray", "Black"],
            "stock": 30,
            "images": ["https://via.placeholder.com/500x500?text=Hiking+Boots"],
            "is_featured": True,
            "is_on_sale": True,
        },
        {
            "name": "Evening Heels",
            "description": "Elegant high heels for special occasions",
            "price": 139.99,
            "discount_price": None,
            "category_id": 1,
            "gender": "women",
            "brand": "Jimmy Choo",
            "sizes": ["5", "6", "7", "8", "9", "10"],
            "colors": ["Black", "Silver", "Gold", "Red"],
            "stock": 20,
            "images": ["https://via.placeholder.com/500x500?text=Evening+Heels"],
            "is_featured": False,
            "is_on_sale": False,
        },
        {
            "name": "Casual Canvas Shoes",
            "description": "Lightweight canvas shoes perfect for everyday casual wear",
            "price": 59.99,
            "discount_price": 39.99,
            "category_id": 1,
            "gender": "men",
            "brand": "Vans",
            "sizes": ["6", "7", "8", "9", "10", "11", "12", "13"],
            "colors": ["White", "Black", "Blue", "Red"],
            "stock": 60,
            "images": ["https://via.placeholder.com/500x500?text=Canvas+Shoes"],
            "is_featured": False,
            "is_on_sale": True,
        },
        # Shirts
        {
            "name": "Classic Cotton T-Shirt",
            "description": "High-quality cotton t-shirt comfortable for everyday wear",
            "price": 29.99,
            "discount_price": None,
            "category_id": 2,
            "gender": "men",
            "brand": "Gap",
            "sizes": ["XS", "S", "M", "L", "XL", "XXL"],
            "colors": ["White", "Black", "Blue", "Gray"],
            "stock": 80,
            "images": ["https://via.placeholder.com/500x500?text=T-Shirt"],
            "is_featured": False,
            "is_on_sale": False,
        },
        {
            "name": "Silk Blouse",
            "description": "Elegant silk blouse for professional and casual occasions",
            "price": 89.99,
            "discount_price": 59.99,
            "category_id": 2,
            "gender": "women",
            "brand": "Hugo Boss",
            "sizes": ["XS", "S", "M", "L", "XL"],
            "colors": ["White", "Black", "Blue", "Burgundy"],
            "stock": 25,
            "images": ["https://via.placeholder.com/500x500?text=Blouse"],
            "is_featured": True,
            "is_on_sale": True,
        },
        # Pants
        {
            "name": "Slim Fit Jeans",
            "description": "Modern slim fit jeans with stretch comfort",
            "price": 79.99,
            "discount_price": 59.99,
            "category_id": 3,
            "gender": "men",
            "brand": "Levi's",
            "sizes": ["28", "30", "32", "34", "36", "38", "40"],
            "colors": ["Dark Blue", "Light Blue", "Black"],
            "stock": 50,
            "images": ["https://via.placeholder.com/500x500?text=Jeans"],
            "is_featured": False,
            "is_on_sale": True,
        },
        {
            "name": "Yoga Leggings",
            "description": "High-waist yoga leggings with moisture-wicking fabric",
            "price": 69.99,
            "discount_price": None,
            "category_id": 3,
            "gender": "women",
            "brand": "Lululemon",
            "sizes": ["XS", "S", "M", "L", "XL"],
            "colors": ["Black", "Navy", "Burgundy", "Gray"],
            "stock": 35,
            "images": ["https://via.placeholder.com/500x500?text=Leggings"],
            "is_featured": True,
            "is_on_sale": False,
        },
    ]

    for product_data in products_data:
        product = Product(
            name=product_data["name"],
            description=product_data["description"],
            price=product_data["price"],
            discount_price=product_data["discount_price"],
            category_id=product_data["category_id"],
            gender=product_data["gender"],
            brand=product_data["brand"],
            sizes=product_data["sizes"],
            colors=product_data["colors"],
            stock=product_data["stock"],
            images=product_data["images"],
            is_featured=product_data["is_featured"],
            is_on_sale=product_data["is_on_sale"],
        )
        db.add(product)

    # Create sample users
    sample_users = [
        User(
            email="user@example.com",
            full_name="John Doe",
            hashed_password=get_password_hash("password123"),
            phone="+1234567890",
            address="123 Main St",
            city="New York",
            postal_code="10001",
            country="USA",
        ),
        User(
            email="jane@example.com",
            full_name="Jane Smith",
            hashed_password=get_password_hash("password123"),
            phone="+0987654321",
            address="456 Oak Ave",
            city="Los Angeles",
            postal_code="90001",
            country="USA",
        ),
    ]

    db.add_all(sample_users)

    db.commit()
    print("Database seeded successfully!")
    print(f"Created {len(categories)} categories")
    print(f"Created {len(products_data)} products")
    print(f"Created {len(sample_users)} users")
    db.close()


if __name__ == "__main__":
    seed_database()
