"""
Email templates for various notification types.
"""
from datetime import date
from typing import Dict, Any

from app.models.reservation import Reservation
from app.models.client import Client
from app.models.establishment import Establishment
from app.models.folio import Folio


class EmailTemplates:
    """HTML email templates for notifications."""

    @staticmethod
    def _base_template(content: str, establishment: Establishment) -> str:
        """Base HTML template wrapper."""
        return f"""
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {{
            font-family: Arial, sans-serif;
            line-height: 1.6;
            color: #333;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
        }}
        .header {{
            background-color: #2563eb;
            color: white;
            padding: 20px;
            text-align: center;
            border-radius: 8px 8px 0 0;
        }}
        .content {{
            background-color: #f9fafb;
            padding: 30px;
            border-radius: 0 0 8px 8px;
        }}
        .info-box {{
            background-color: white;
            padding: 20px;
            margin: 20px 0;
            border-radius: 8px;
            border-left: 4px solid #2563eb;
        }}
        .info-row {{
            display: flex;
            justify-content: space-between;
            padding: 8px 0;
            border-bottom: 1px solid #e5e7eb;
        }}
        .info-row:last-child {{
            border-bottom: none;
        }}
        .info-label {{
            font-weight: bold;
            color: #6b7280;
        }}
        .info-value {{
            color: #111827;
        }}
        .footer {{
            text-align: center;
            color: #6b7280;
            font-size: 12px;
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #e5e7eb;
        }}
        .button {{
            display: inline-block;
            padding: 12px 24px;
            background-color: #2563eb;
            color: white;
            text-decoration: none;
            border-radius: 6px;
            margin: 10px 0;
        }}
    </style>
</head>
<body>
    <div class="header">
        <h1>{establishment.name}</h1>
    </div>
    <div class="content">
        {content}
    </div>
    <div class="footer">
        <p>
            {establishment.name}<br>
            {establishment.address}, {establishment.city}<br>
            Tél: {establishment.phone} | Email: {establishment.email}
        </p>
        <p>Cet email a été envoyé automatiquement, merci de ne pas y répondre.</p>
    </div>
</body>
</html>
"""

    @staticmethod
    def reservation_confirmation(
        reservation: Reservation,
        client: Client,
        establishment: Establishment
    ) -> tuple[str, str]:
        """
        Generate confirmation email template.

        Returns:
            Tuple of (subject, html_body)
        """
        subject = f"Confirmation de réservation - {reservation.confirmation_number}"

        content = f"""
        <h2>Réservation confirmée !</h2>
        <p>Cher(e) {client.full_name},</p>
        <p>Nous avons le plaisir de confirmer votre réservation au {establishment.name}.</p>

        <div class="info-box">
            <h3>Détails de votre réservation</h3>
            <div class="info-row">
                <span class="info-label">Numéro de confirmation</span>
                <span class="info-value">{reservation.confirmation_number}</span>
            </div>
            <div class="info-row">
                <span class="info-label">Arrivée</span>
                <span class="info-value">{reservation.check_in_date.strftime('%d/%m/%Y')}</span>
            </div>
            <div class="info-row">
                <span class="info-label">Départ</span>
                <span class="info-value">{reservation.check_out_date.strftime('%d/%m/%Y')}</span>
            </div>
            <div class="info-row">
                <span class="info-label">Nombre de nuits</span>
                <span class="info-value">{reservation.nights}</span>
            </div>
            <div class="info-row">
                <span class="info-label">Chambre</span>
                <span class="info-value">{reservation.room.room_number if reservation.room else 'À attribuer'}</span>
            </div>
            <div class="info-row">
                <span class="info-label">Nombre de personnes</span>
                <span class="info-value">{reservation.adults} adulte(s), {reservation.children} enfant(s)</span>
            </div>
            <div class="info-row">
                <span class="info-label">Tarif total</span>
                <span class="info-value">{reservation.total_price:.2f} {reservation.currency}</span>
            </div>
        </div>

        {f'<p><strong>Demandes spéciales :</strong> {reservation.special_requests}</p>' if reservation.special_requests else ''}

        <p>
            <strong>Check-in :</strong> à partir de {establishment.default_checkin_time}<br>
            <strong>Check-out :</strong> avant {establishment.default_checkout_time}
        </p>

        <p>Nous vous remercions de votre confiance et nous réjouissons de vous accueillir.</p>

        <p>Cordialement,<br>L'équipe de {establishment.name}</p>
        """

        html_body = EmailTemplates._base_template(content, establishment)
        return subject, html_body

    @staticmethod
    def reservation_modification(
        reservation: Reservation,
        client: Client,
        establishment: Establishment
    ) -> tuple[str, str]:
        """
        Generate modification email template.

        Returns:
            Tuple of (subject, html_body)
        """
        subject = f"Modification de réservation - {reservation.confirmation_number}"

        content = f"""
        <h2>Réservation modifiée</h2>
        <p>Cher(e) {client.full_name},</p>
        <p>Votre réservation a été modifiée avec succès.</p>

        <div class="info-box">
            <h3>Nouvelles informations</h3>
            <div class="info-row">
                <span class="info-label">Numéro de confirmation</span>
                <span class="info-value">{reservation.confirmation_number}</span>
            </div>
            <div class="info-row">
                <span class="info-label">Arrivée</span>
                <span class="info-value">{reservation.check_in_date.strftime('%d/%m/%Y')}</span>
            </div>
            <div class="info-row">
                <span class="info-label">Départ</span>
                <span class="info-value">{reservation.check_out_date.strftime('%d/%m/%Y')}</span>
            </div>
            <div class="info-row">
                <span class="info-label">Nombre de nuits</span>
                <span class="info-value">{reservation.nights}</span>
            </div>
            <div class="info-row">
                <span class="info-label">Tarif total</span>
                <span class="info-value">{reservation.total_price:.2f} {reservation.currency}</span>
            </div>
        </div>

        <p>Si vous avez des questions, n'hésitez pas à nous contacter.</p>

        <p>Cordialement,<br>L'équipe de {establishment.name}</p>
        """

        html_body = EmailTemplates._base_template(content, establishment)
        return subject, html_body

    @staticmethod
    def reservation_cancellation(
        reservation: Reservation,
        client: Client,
        establishment: Establishment
    ) -> tuple[str, str]:
        """
        Generate cancellation email template.

        Returns:
            Tuple of (subject, html_body)
        """
        subject = f"Annulation de réservation - {reservation.confirmation_number}"

        content = f"""
        <h2>Réservation annulée</h2>
        <p>Cher(e) {client.full_name},</p>
        <p>Nous vous confirmons l'annulation de votre réservation.</p>

        <div class="info-box">
            <h3>Réservation annulée</h3>
            <div class="info-row">
                <span class="info-label">Numéro de confirmation</span>
                <span class="info-value">{reservation.confirmation_number}</span>
            </div>
            <div class="info-row">
                <span class="info-label">Dates initialement prévues</span>
                <span class="info-value">
                    Du {reservation.check_in_date.strftime('%d/%m/%Y')}
                    au {reservation.check_out_date.strftime('%d/%m/%Y')}
                </span>
            </div>
        </div>

        <p>Nous espérons avoir le plaisir de vous accueillir prochainement.</p>

        <p>Cordialement,<br>L'équipe de {establishment.name}</p>
        """

        html_body = EmailTemplates._base_template(content, establishment)
        return subject, html_body

    @staticmethod
    def reservation_reminder(
        reservation: Reservation,
        client: Client,
        establishment: Establishment
    ) -> tuple[str, str]:
        """
        Generate reminder email template.

        Returns:
            Tuple of (subject, html_body)
        """
        subject = f"Rappel : Votre arrivée demain - {reservation.confirmation_number}"

        content = f"""
        <h2>Votre arrivée est pour bientôt !</h2>
        <p>Cher(e) {client.full_name},</p>
        <p>Nous avons hâte de vous accueillir demain au {establishment.name}.</p>

        <div class="info-box">
            <h3>Rappel de votre réservation</h3>
            <div class="info-row">
                <span class="info-label">Numéro de confirmation</span>
                <span class="info-value">{reservation.confirmation_number}</span>
            </div>
            <div class="info-row">
                <span class="info-label">Arrivée</span>
                <span class="info-value">{reservation.check_in_date.strftime('%d/%m/%Y')} à partir de {establishment.default_checkin_time}</span>
            </div>
            <div class="info-row">
                <span class="info-label">Départ</span>
                <span class="info-value">{reservation.check_out_date.strftime('%d/%m/%Y')} avant {establishment.default_checkout_time}</span>
            </div>
            <div class="info-row">
                <span class="info-label">Chambre</span>
                <span class="info-value">{reservation.room.room_number if reservation.room else 'À attribuer'}</span>
            </div>
        </div>

        <p><strong>Informations utiles :</strong></p>
        <ul>
            <li>Check-in à partir de {establishment.default_checkin_time}</li>
            <li>Merci d'apporter une pièce d'identité valide</li>
            <li>Parking disponible sur place</li>
        </ul>

        <p>Bon voyage et à très bientôt !</p>

        <p>Cordialement,<br>L'équipe de {establishment.name}</p>
        """

        html_body = EmailTemplates._base_template(content, establishment)
        return subject, html_body

    @staticmethod
    def check_in_confirmation(
        reservation: Reservation,
        client: Client,
        establishment: Establishment
    ) -> tuple[str, str]:
        """
        Generate check-in confirmation email template.

        Returns:
            Tuple of (subject, html_body)
        """
        subject = f"Bienvenue au {establishment.name} !"

        content = f"""
        <h2>Bienvenue !</h2>
        <p>Cher(e) {client.full_name},</p>
        <p>Nous sommes ravis de vous accueillir au {establishment.name}.</p>

        <div class="info-box">
            <h3>Votre séjour</h3>
            <div class="info-row">
                <span class="info-label">Chambre</span>
                <span class="info-value">{reservation.room.room_number if reservation.room else '-'}</span>
            </div>
            <div class="info-row">
                <span class="info-label">Check-out</span>
                <span class="info-value">{reservation.check_out_date.strftime('%d/%m/%Y')} avant {establishment.default_checkout_time}</span>
            </div>
        </div>

        <p><strong>Services disponibles :</strong></p>
        <ul>
            <li>Wi-Fi gratuit</li>
            <li>Petit-déjeuner de 7h à 10h</li>
            <li>Service de chambre jusqu'à 22h</li>
        </ul>

        <p>N'hésitez pas à nous contacter pour toute demande.</p>

        <p>Excellent séjour parmi nous !<br>L'équipe de {establishment.name}</p>
        """

        html_body = EmailTemplates._base_template(content, establishment)
        return subject, html_body

    @staticmethod
    def invoice_email(
        folio: Folio,
        client: Client,
        establishment: Establishment
    ) -> tuple[str, str]:
        """
        Generate invoice email template.

        Returns:
            Tuple of (subject, html_body)
        """
        subject = f"Votre facture - {folio.folio_number}"

        content = f"""
        <h2>Votre facture</h2>
        <p>Cher(e) {client.full_name},</p>
        <p>Nous vous remercions pour votre séjour au {establishment.name}.</p>

        <div class="info-box">
            <h3>Récapitulatif</h3>
            <div class="info-row">
                <span class="info-label">Numéro de facture</span>
                <span class="info-value">{folio.folio_number}</span>
            </div>
            <div class="info-row">
                <span class="info-label">Total charges</span>
                <span class="info-value">{folio.total_charges:.2f} {folio.currency}</span>
            </div>
            <div class="info-row">
                <span class="info-label">Paiements</span>
                <span class="info-value">{folio.total_payments:.2f} {folio.currency}</span>
            </div>
            <div class="info-row">
                <span class="info-label"><strong>Solde</strong></span>
                <span class="info-value"><strong>{folio.balance:.2f} {folio.currency}</strong></span>
            </div>
        </div>

        <p>Vous trouverez en pièce jointe le détail de votre facture.</p>

        <p>Nous espérons vous revoir très bientôt !</p>

        <p>Cordialement,<br>L'équipe de {establishment.name}</p>
        """

        html_body = EmailTemplates._base_template(content, establishment)
        return subject, html_body
