import sys import os from datetime import datetime, timedelta # Add parent directory to path to import from src sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from sqlalchemy.orm import Session from src.shared.config.database import SessionLocal from src.loyalty.models.promotion import Promotion, DiscountType def seed_promotions(db: Session): """Seed promotions that match the homepage promotion codes""" # Calculate dates relative to current UTC date # Use UTC consistently to match validation logic now = datetime.utcnow() # Start date: Start of today (00:00:00) to ensure promotion is immediately active start_date = datetime(now.year, now.month, now.day, 0, 0, 0) # End dates: future dates at end of day (23:59:59) next_month_date = now + timedelta(days=30) next_month = datetime(next_month_date.year, next_month_date.month, next_month_date.day, 23, 59, 59) next_3_months_date = now + timedelta(days=90) next_3_months = datetime(next_3_months_date.year, next_3_months_date.month, next_3_months_date.day, 23, 59, 59) next_6_months_date = now + timedelta(days=180) next_6_months = datetime(next_6_months_date.year, next_6_months_date.month, next_6_months_date.day, 23, 59, 59) promotions_data = [ { 'code': 'EARLYBIRD20', 'name': 'Early Bird Special', 'description': 'Book 30 days in advance and save 20% on your stay. Perfect for planning ahead!', 'discount_type': DiscountType.percentage, 'discount_value': 20.00, 'start_date': start_date, 'end_date': next_3_months, 'is_active': True, 'min_booking_amount': None, 'max_discount_amount': None, 'usage_limit': None, }, { 'code': 'WEEKEND30', 'name': 'Weekend Getaway', 'description': 'Perfect weekend escape with complimentary breakfast and spa access. Relax and unwind!', 'discount_type': DiscountType.percentage, 'discount_value': 30.00, 'start_date': start_date, 'end_date': next_month, 'is_active': True, 'min_booking_amount': None, 'max_discount_amount': None, 'usage_limit': None, }, { 'code': 'LUXURY200', 'name': 'Luxury Suite Package', 'description': 'Experience our premium suites with exclusive amenities, fine dining, and concierge service', 'discount_type': DiscountType.fixed_amount, 'discount_value': 200.00, 'start_date': start_date, 'end_date': next_6_months, 'is_active': True, 'min_booking_amount': None, # No minimum for now 'max_discount_amount': None, 'usage_limit': None, }, { 'code': 'HONEYMOON25', 'name': 'Honeymoon Special', 'description': 'Romantic getaway with champagne, flowers, special amenities, and complimentary room upgrade', 'discount_type': DiscountType.percentage, 'discount_value': 25.00, 'start_date': start_date, 'end_date': next_3_months, 'is_active': True, 'min_booking_amount': None, 'max_discount_amount': None, 'usage_limit': None, }, { 'code': 'FAMILY15', 'name': 'Family Fun Package', 'description': 'Perfect for families! Includes family room, kids activities, and complimentary meals for children under 12', 'discount_type': DiscountType.percentage, 'discount_value': 15.00, 'start_date': start_date, 'end_date': next_6_months, 'is_active': True, 'min_booking_amount': None, 'max_discount_amount': None, 'usage_limit': None, }, { 'code': 'BUSINESS10', 'name': 'Business Traveler', 'description': 'Extended stay discounts for business travelers. Includes high-speed WiFi, workspace, and airport transfer', 'discount_type': DiscountType.percentage, 'discount_value': 10.00, 'start_date': start_date, 'end_date': next_3_months, 'is_active': True, 'min_booking_amount': None, 'max_discount_amount': None, 'usage_limit': None, }, ] created_count = 0 updated_count = 0 for promo_data in promotions_data: existing = db.query(Promotion).filter(Promotion.code == promo_data['code']).first() if existing: # Update existing promotion for key, value in promo_data.items(): if key != 'code': # Don't update the code setattr(existing, key, value) existing.updated_at = datetime.utcnow() updated_count += 1 print(f'✓ Updated promotion: {promo_data["code"]}') else: # Create new promotion promotion = Promotion(**promo_data) db.add(promotion) created_count += 1 print(f'✓ Created promotion: {promo_data["code"]}') db.commit() print(f'\n✓ Promotions seeded: {created_count} created, {updated_count} updated') def main(): db: Session = SessionLocal() try: print('=' * 80) print('SEEDING PROMOTIONS') print('=' * 80) print() seed_promotions(db) print('\n' + '=' * 80) print('✓ All promotions seeded successfully!') print('=' * 80) except Exception as e: db.rollback() print(f'\n✗ Error seeding promotions: {e}') import traceback traceback.print_exc() raise finally: db.close() if __name__ == '__main__': main()