from datetime import datetime

class NotificationFormatter:
    def __init__(self, currency='EUR', currency_symbol='€'):
        self.currency = currency
        self.currency_symbol = currency_symbol
    
    def format_admin_approval_request(self, booking_data):
        guest_email = booking_data.get('guest_email', booking_data.get('user_email', 'N/A'))
        guest_phone = booking_data.get('guest_phone', 'N/A')
        # Handle both created_at (from database) and submitted_at (for backwards compatibility)
        created_at = booking_data.get('created_at', booking_data.get('submitted_at'))
        if created_at:
            # If it's ISO format, try to parse and format nicely
            try:
                if 'T' in str(created_at):
                    dt_obj = datetime.fromisoformat(str(created_at).replace('Z', '+00:00'))
                    submitted_at = dt_obj.strftime('%Y-%m-%d %H:%M:%S')
                else:
                    submitted_at = created_at
            except:
                submitted_at = created_at
        else:
            submitted_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        
        special_requests = booking_data.get('special_requests', 'None')
        
        message = f"""
╔══════════════════════════════════════════════════════════╗
║          BOOKING APPROVAL REQUEST                         ║
╚══════════════════════════════════════════════════════════╝

Dear Administrator,

A new booking request requires your review and approval.

BOOKING DETAILS
────────────────────────────────────────────────────────
Booking ID:      {booking_data.get('booking_id')}
Property:        {booking_data.get('property_name', booking_data.get('accommodation_title', 'N/A'))}
Guest Name:      {booking_data.get('guest_name', booking_data.get('user_name', 'N/A'))}
Guest Email:     {guest_email}
Guest Phone:     {guest_phone}
Check-in:        {booking_data.get('check_in_date')}
Check-out:       {booking_data.get('check_out_date')}
Guests:          {booking_data.get('num_guests', 1)}
Total Price:     {self.currency_symbol}{booking_data.get('total_price'):.2f} {self.currency}
Submitted:       {submitted_at}
────────────────────────────────────────────────────────

SPECIAL REQUESTS
────────────────────────────────────────────────────────
{special_requests}
────────────────────────────────────────────────────────

ACTION REQUIRED
Please review this booking request and take one of the following actions:

✓ CONFIRM - Approve the booking and send confirmation to guest
✗ REJECT - Decline the booking and notify the guest

Please respond within 24 hours to ensure timely guest communication.

Best regards,
Booking System
────────────────────────────────────────────────────────
Request received: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
"""
        return message.strip()