import sys
import os
from sqlmodel import Session, select

# Add parent directory to path to import app
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from app.core.database import engine
from app.models.business import Business
from app.models.coupon import Coupon

def seed_promos():
    with Session(engine) as session:
        # 1. Check if we already have these businesses
        biz_name = "Pizzeria El Centro"
        biz = session.exec(select(Business).where(Business.name == biz_name)).first()
        
        if not biz:
            biz = Business(
                name=biz_name,
                category="Restaurante",
                address="Calle Central, David",
                phone="775-1234",
                latitude=8.4272,
                longitude=-82.4300,
                image_url="/uploads/businesses/pizzeria.jpg"
            )
            session.add(biz)
            session.commit()
            session.refresh(biz)
            print(f"Created business: {biz_name}")
        else:
            # Update coords if needed
            biz.latitude = 8.4272
            biz.longitude = -82.4300
            session.add(biz)
            session.commit()
            print(f"Updated business: {biz_name}")

        # 2. Add a coupon for this business
        coupon_title = "Pizzas 2x1 Martes"
        coupon = session.exec(select(Coupon).where(Coupon.title == coupon_title)).first()
        
        if not coupon:
            coupon = Coupon(
                title=coupon_title,
                business_id=biz.id,
                business_name=biz.name,
                business_address=biz.address,
                description="Lleva 2 pizzas al precio de 1 todos los martes.",
                category="Restaurante",
                discount_percentage=50,
                is_active=True,
                image_url=biz.image_url
            )
            session.add(coupon)
            session.commit()
            print(f"Created coupon: {coupon_title}")

        # 3. Add second business
        biz_name2 = "Heladeria Glacial"
        biz2 = session.exec(select(Business).where(Business.name == biz_name2)).first()
        
        if not biz2:
            biz2 = Business(
                name=biz_name2,
                category="Restaurante",
                address="Plaza Terronal, David",
                phone="774-5678",
                latitude=8.4350,
                longitude=-82.4250,
                image_url="/uploads/businesses/icecream.jpg"
            )
            session.add(biz2)
            session.commit()
            session.refresh(biz2)
            print(f"Created business: {biz_name2}")
        else:
            biz2.latitude = 8.4350
            biz2.longitude = -82.4250
            session.add(biz2)
            session.commit()
            print(f"Updated business: {biz_name2}")

        # 4. Add a coupon for second business
        coupon_title2 = "30% en Helados"
        coupon2 = session.exec(select(Coupon).where(Coupon.title == coupon_title2)).first()
        
        if not coupon2:
            coupon2 = Coupon(
                title=coupon_title2,
                business_id=biz2.id,
                business_name=biz2.name,
                business_address=biz2.address,
                description="30% de descuento en el segundo helado.",
                category="Restaurante",
                discount_percentage=30,
                is_active=True,
                image_url=biz2.image_url
            )
            session.add(coupon2)
            session.commit()
            print(f"Created coupon: {coupon_title2}")

if __name__ == "__main__":
    seed_promos()
