import sys
import os
from datetime import datetime, timedelta
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
from sqlalchemy.orm import Session
from src.config.database import SessionLocal
from src.models.banner import Banner
from src.models.system_settings import SystemSettings
from src.models.user import User

def seed_banners(db: Session):
    print('Seeding banners...')
    admin_user = db.query(User).filter(User.email == 'admin@hotel.com').first()
    admin_id = admin_user.id if admin_user else None
    existing_banners = db.query(Banner).all()
    if existing_banners:
        for banner in existing_banners:
            db.delete(banner)
        db.commit()
        print(f'  ✓ Removed {len(existing_banners)} existing banner(s)')
    banners_data = [{'title': 'Welcome to Unparalleled Luxury', 'description': 'Where timeless elegance meets modern sophistication. Experience the pinnacle of hospitality in our award-winning luxury hotel.', 'image_url': 'https://images.unsplash.com/photo-1564501049412-61c2a3083791?w=1920', 'link_url': '/rooms', 'position': 'home', 'display_order': 1, 'is_active': True, 'start_date': datetime.utcnow() - timedelta(days=30), 'end_date': datetime.utcnow() + timedelta(days=365)}, {'title': 'Exclusive Presidential Suites', 'description': 'Indulge in our most opulent accommodations. Spacious suites with panoramic views, private terraces, and personalized butler service.', 'image_url': 'https://images.unsplash.com/photo-1590490360182-c33d57733427?w=1920', 'link_url': '/rooms', 'position': 'home', 'display_order': 2, 'is_active': True, 'start_date': datetime.utcnow() - timedelta(days=7), 'end_date': datetime.utcnow() + timedelta(days=365)}, {'title': 'World-Class Spa & Wellness', 'description': 'Rejuvenate your mind, body, and soul. Our award-winning spa offers bespoke treatments using the finest luxury products.', 'image_url': 'https://images.unsplash.com/photo-1544161515-4ab6ce6db874?w=1920', 'link_url': '/services', 'position': 'home', 'display_order': 3, 'is_active': True, 'start_date': datetime.utcnow() - timedelta(days=1), 'end_date': datetime.utcnow() + timedelta(days=365)}, {'title': 'Michelin-Starred Culinary Excellence', 'description': 'Savor extraordinary flavors crafted by world-renowned chefs. Our fine dining restaurants offer an unforgettable gastronomic journey.', 'image_url': 'https://images.unsplash.com/photo-1517248135467-4c7edcad34c4?w=1920', 'link_url': '/services', 'position': 'home', 'display_order': 4, 'is_active': True, 'start_date': datetime.utcnow(), 'end_date': datetime.utcnow() + timedelta(days=365)}, {'title': 'Private Yacht & Exclusive Experiences', 'description': 'Create unforgettable memories with our curated luxury experiences. From private yacht charters to exclusive cultural tours.', 'image_url': 'https://images.unsplash.com/photo-1544551763-46a013bb70d5?w=1920', 'link_url': '/services', 'position': 'home', 'display_order': 5, 'is_active': True, 'start_date': datetime.utcnow() - timedelta(days=15), 'end_date': datetime.utcnow() + timedelta(days=365)}]
    for banner_data in banners_data:
        new_banner = Banner(**banner_data)
        db.add(new_banner)
        print(f'  ✓ Created banner: {banner_data['title']}')
    db.commit()
    print('✓ Banners seeded successfully!\n')

def seed_company_info(db: Session):
    print('Seeding company information...')
    admin_user = db.query(User).filter(User.email == 'admin@hotel.com').first()
    admin_id = admin_user.id if admin_user else None
    company_settings = [{'key': 'company_name', 'value': 'Luxury Hotel', 'description': 'Company name displayed throughout the application'}, {'key': 'company_tagline', 'value': 'Experience Unparalleled Elegance', 'description': 'Company tagline or slogan'}, {'key': 'company_logo_url', 'value': '', 'description': 'URL to company logo image (upload via admin dashboard)'}, {'key': 'company_favicon_url', 'value': '', 'description': 'URL to company favicon image (upload via admin dashboard)'}, {'key': 'company_phone', 'value': '+1 (555) 123-4567', 'description': 'Company contact phone number'}, {'key': 'company_email', 'value': 'info@luxuryhotel.com', 'description': 'Company contact email address'}, {'key': 'company_address', 'value': '123 Luxury Avenue, Premium District, City 12345, Country', 'description': 'Company physical address'}, {'key': 'tax_rate', 'value': '10.0', 'description': 'Default tax rate percentage (e.g., 10.0 for 10%)'}, {'key': 'platform_currency', 'value': 'EUR', 'description': 'Platform-wide currency setting for displaying prices'}]
    for setting_data in company_settings:
        existing = db.query(SystemSettings).filter(SystemSettings.key == setting_data['key']).first()
        if existing:
            existing.value = setting_data['value']
            existing.description = setting_data['description']
            if admin_id:
                existing.updated_by_id = admin_id
            print(f'  ✓ Updated setting: {setting_data['key']}')
        else:
            new_setting = SystemSettings(key=setting_data['key'], value=setting_data['value'], description=setting_data['description'], updated_by_id=admin_id)
            db.add(new_setting)
            print(f'  ✓ Created setting: {setting_data['key']}')
    db.commit()
    print('✓ Company information seeded successfully!\n')

def main():
    db: Session = SessionLocal()
    try:
        print('=' * 80)
        print('SEEDING BANNERS AND COMPANY INFORMATION')
        print('=' * 80)
        print()
        seed_banners(db)
        seed_company_info(db)
        print('=' * 80)
        print('✓ All data seeded successfully!')
        print('=' * 80)
    except Exception as e:
        db.rollback()
        print(f'\n✗ Error seeding data: {e}')
        import traceback
        traceback.print_exc()
        raise
    finally:
        db.close()
if __name__ == '__main__':
    main()