@page "/Contact"
@using System.ComponentModel.DataAnnotations
@using System.Text
@using ModelsLib
@inject EmailService mailService
@inject IJSRuntime JS
@rendermode InteractiveServer
@inject NavigationManager Navigation

<!-- Contact -->
<section class="contact-section">

    <MudContainer MaxWidth="MaxWidth.Small" Class="text-center">

        <!-- Titre -->
        <MudText Typo="Typo.h5" Class="mb-2">
            Contact
        </MudText>

        <!-- Sous-titre -->
        <MudText Typo="Typo.body1" Class="mb-6">
            Une question ? Un projet ? Envie d'une collaboration ? Écrivez-moi directement.
        </MudText>

        <!-- Formulaire -->
        <EditForm EditContext="@editContext" OnValidSubmit="EnvoyerMessage">
            <DataAnnotationsValidator />
            <ValidationSummary />

            <MudStack Spacing="2">

                <MudTextField @bind-Value="contact.Name"
                              Label="Votre nom"
                              Variant="Variant.Outlined"
                              Required="true" />

                <MudTextField @bind-Value="contact.Email"
                              Label="Votre adresse mail"
                              Variant="Variant.Outlined"
                              Required="true" />

                <MudTextField @bind-Value="contact.Title"
                              Label="Sujet du message"
                              Variant="Variant.Outlined" />

                <MudTextField @bind-Value="contact.Message"
                              Label="Message"
                              Variant="Variant.Outlined"
                              Lines="6"
                              TextArea="true" />

                <MudButton ButtonType="ButtonType.Submit"
                           Variant="Variant.Filled"
                           Color="Color.Primary"
                           Class="mt-4">
                    Laisser un message
                </MudButton>

            </MudStack>
        </EditForm>

        <!-- Message succès -->
        @if (messageEnvoye)
        {
            <MudAlert Severity="Severity.Success" Class="mt-6">
                ✅ Message envoyé, merci !
            </MudAlert>
        }

    </MudContainer>

</section>

@code {

    #region properties
    private ContactModel contact = new();
    private EditContext? editContext;
    private bool messageEnvoye = false;
    #endregion

    /// <summary>
    /// Chargement de la page
    /// </summary>
    protected override async Task OnInitializedAsync()
    {
        editContext = new EditContext(contact);
    }

    /// <summary>
    /// Méthode d'envoi de mail
    /// </summary>
    private void EnvoyerMessage()
    {
        var body = new StringBuilder();
        body.Append("<p>").Append(contact.Message).Append("</p>");
        mailService.EnvoyerMailContactAsync(contact.Email,contact.Name+" : " +contact.Title, body).GetAwaiter();

        messageEnvoye = true;
        contact = new ContactModel();
        editContext = new EditContext(contact);
    }
}
