#!/usr/bin/env python3
"""
Seeder Runner
Run all seeders or specific seeders
"""
import sys
import argparse
from pathlib import Path

# Add parent directory to path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from homepage_seeder import seed_homepage
from about_seeder import seed_about_page
from contact_seeder import seed_contact_page
from privacy_seeder import seed_privacy_page
from faq_seeder import seed_faq_page
from accessibility_seeder import seed_accessibility_page
from refunds_seeder import seed_refunds_page
from terms_seeder import seed_terms_page
from cancellation_seeder import seed_cancellation_page
from footer_seeder import seed_footer_page
from banner_seeder import seed_banners
from user_seeder import seed_users_and_roles
from room_seeder import seed_rooms_and_types
from service_seeder import seed_services
from blog_seeder import seed_blog_posts
from settings_seeder import seed_settings
from src.shared.config.database import SessionLocal
from src.shared.config.logging_config import get_logger

logger = get_logger(__name__)


def main():
    parser = argparse.ArgumentParser(description='Run database seeders')
    parser.add_argument(
        '--seeder',
        choices=['homepage', 'about', 'contact', 'privacy', 'faq', 'accessibility', 'refunds', 'terms', 'cancellation', 'footer', 'banners', 'users', 'rooms', 'services', 'blog', 'settings', 'all'],
        default='all',
        help='Which seeder to run (default: all)'
    )
    parser.add_argument(
        '--clear-banners',
        action='store_true',
        help='Clear existing banners before seeding (only applies to banners seeder)'
    )
    parser.add_argument(
        '--clear-rooms',
        action='store_true',
        help='Clear existing rooms before seeding (only applies to rooms seeder)'
    )
    parser.add_argument(
        '--clear-services',
        action='store_true',
        help='Clear existing services before seeding (only applies to services seeder)'
    )
    parser.add_argument(
        '--clear-blog',
        action='store_true',
        help='Clear existing blog posts before seeding (only applies to blog seeder)'
    )
    parser.add_argument(
        '--clear-settings',
        action='store_true',
        help='Clear existing settings before seeding (only applies to settings seeder)'
    )
    parser.add_argument(
        '--admin-email',
        default='admin@hotel.com',
        help='Admin user email (default: admin@hotel.com)'
    )
    parser.add_argument(
        '--admin-password',
        default='admin123',
        help='Admin user password (default: admin123)'
    )
    parser.add_argument(
        '--admin-name',
        default='Admin User',
        help='Admin user full name (default: Admin User)'
    )
    
    args = parser.parse_args()
    
    db = SessionLocal()
    try:
        # Always seed users/roles first if running all or users seeder
        if args.seeder == 'users' or args.seeder == 'all':
            logger.info('Running user and role seeder...')
            seed_users_and_roles(
                db,
                admin_email=args.admin_email,
                admin_password=args.admin_password,
                admin_name=args.admin_name
            )
            logger.info('User and role seeder completed!')
        
        if args.seeder == 'homepage' or args.seeder == 'all':
            logger.info('Running homepage seeder...')
            seed_homepage(db)
            logger.info('Homepage seeder completed!')
        
        if args.seeder == 'about' or args.seeder == 'all':
            logger.info('Running about page seeder...')
            seed_about_page(db)
            logger.info('About page seeder completed!')
        
        if args.seeder == 'contact' or args.seeder == 'all':
            logger.info('Running contact page seeder...')
            seed_contact_page(db)
            logger.info('Contact page seeder completed!')
        
        if args.seeder == 'privacy' or args.seeder == 'all':
            logger.info('Running privacy policy page seeder...')
            seed_privacy_page(db)
            logger.info('Privacy policy page seeder completed!')
        
        if args.seeder == 'faq' or args.seeder == 'all':
            logger.info('Running FAQ page seeder...')
            seed_faq_page(db)
            logger.info('FAQ page seeder completed!')
        
        if args.seeder == 'accessibility' or args.seeder == 'all':
            logger.info('Running accessibility page seeder...')
            seed_accessibility_page(db)
            logger.info('Accessibility page seeder completed!')
        
        if args.seeder == 'refunds' or args.seeder == 'all':
            logger.info('Running refunds policy page seeder...')
            seed_refunds_page(db)
            logger.info('Refunds policy page seeder completed!')
        
        if args.seeder == 'terms' or args.seeder == 'all':
            logger.info('Running terms of use page seeder...')
            seed_terms_page(db)
            logger.info('Terms of use page seeder completed!')
        
        if args.seeder == 'cancellation' or args.seeder == 'all':
            logger.info('Running cancellation policy page seeder...')
            seed_cancellation_page(db)
            logger.info('Cancellation policy page seeder completed!')
        
        if args.seeder == 'footer' or args.seeder == 'all':
            logger.info('Running footer page seeder...')
            seed_footer_page(db)
            logger.info('Footer page seeder completed!')
        
        if args.seeder == 'banners' or args.seeder == 'all':
            logger.info('Running banner seeder...')
            seed_banners(db, clear_existing=args.clear_banners)
            logger.info('Banner seeder completed!')
        
        if args.seeder == 'rooms' or args.seeder == 'all':
            logger.info('Running room seeder...')
            seed_rooms_and_types(db, clear_existing=args.clear_rooms)
            logger.info('Room seeder completed!')
        
        if args.seeder == 'services' or args.seeder == 'all':
            logger.info('Running service seeder...')
            seed_services(db, clear_existing=args.clear_services)
            logger.info('Service seeder completed!')
        
        if args.seeder == 'blog' or args.seeder == 'all':
            logger.info('Running blog seeder...')
            seed_blog_posts(db, clear_existing=args.clear_blog)
            logger.info('Blog seeder completed!')
        
        if args.seeder == 'settings' or args.seeder == 'all':
            logger.info('Running settings seeder...')
            seed_settings(db, clear_existing=args.clear_settings)
            logger.info('Settings seeder completed!')
        
        logger.info('All seeders completed successfully!')
    except Exception as e:
        logger.error(f'Seeder failed: {str(e)}', exc_info=True)
        sys.exit(1)
    finally:
        db.close()


if __name__ == '__main__':
    main()

