@using Indotalent.ConfigBackEnd.Extensions
@using Indotalent.Features.Pipeline.LeadContact
@using Indotalent.Features.Pipeline.LeadContact.Cqrs
@using Indotalent.Shared.Utils
@using MudBlazor
@inject LeadContactService LeadContactService
@inject ISnackbar Snackbar

<MudPaper Elevation="0" Square="true" Class="pa-6 mb-3 d-flex align-center gap-4" Style="border: 1px solid #DCEBFA;">
    <MudIconButton Icon="@Icons.Material.Filled.ArrowBack" OnClick="() => OnCancel.InvokeAsync()" />
    <div>
        <MudText Typo="Typo.h5" Style="font-weight: 900; color: #1a1a1a;">@(ReadOnly ? "Lead Contact Details" : "Edit Lead Contact")</MudText>
        <MudText Typo="Typo.body2" Style="color: #64748b;">@(ReadOnly ? "Viewing pipeline contact specification." : "Modify existing contact information.")</MudText>
    </div>
</MudPaper>

<MudPaper Elevation="0" Outlined="true" Class="pa-8" Style="border-radius: 0px; border: 1px solid #DCEBFA;">
    <MudForm @ref="_form" Model="_model">
        <MudGrid Spacing="3">
            
            <MudItem xs="12"><MudText Typo="Typo.button" Color="Color.Primary" Style="font-weight: 800;">Basic Information</MudText><MudDivider /></MudItem>
            
            <MudItem xs="12" sm="6">
                <MudText Typo="Typo.subtitle2" Class="mb-1">Full Name</MudText>
                <MudTextField @bind-Value="_model.FullName" For="@(() => _model.FullName)" Validation="@(_validator.ValidateValue())" ReadOnly="ReadOnly" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" />
            </MudItem>

            <MudItem xs="12" sm="6">
                <MudText Typo="Typo.subtitle2" Class="mb-1">Lead</MudText>
                <MudSelect T="string"
                           @bind-Value="_model.LeadId"
                           For="@(() => _model.LeadId)"
                           ReadOnly="ReadOnly"
                           Variant="Variant.Outlined"
                           Margin="Margin.Dense"
                           Dense="true"
                           FullWidth="true"
                           AnchorOrigin="Origin.BottomCenter"
                           TransformOrigin="Origin.TopCenter">
                    @foreach (var item in _lookup.Leads)
                    {
                        <MudSelectItem T="string" Value="@item.Id">@item.Name</MudSelectItem>
                    }
                </MudSelect>
            </MudItem>

            <MudItem xs="12">
                <MudText Typo="Typo.subtitle2" Class="mb-1">Description</MudText>
                <MudTextField @bind-Value="_model.Description" ReadOnly="ReadOnly" Variant="Variant.Outlined" Margin="Margin.Dense" Lines="2" FullWidth="true" />
            </MudItem>

            <MudItem xs="12" Class="mt-4"><MudText Typo="Typo.button" Color="Color.Primary" Style="font-weight: 800;">Address Information</MudText><MudDivider /></MudItem>
            
            <MudItem xs="12"><MudText Typo="Typo.subtitle2" Class="mb-1">Street Address</MudText><MudTextField @bind-Value="_model.AddressStreet" ReadOnly="ReadOnly" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
            <MudItem xs="12" sm="3"><MudText Typo="Typo.subtitle2" Class="mb-1">City</MudText><MudTextField @bind-Value="_model.AddressCity" ReadOnly="ReadOnly" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
            <MudItem xs="12" sm="3"><MudText Typo="Typo.subtitle2" Class="mb-1">State</MudText><MudTextField @bind-Value="_model.AddressState" ReadOnly="ReadOnly" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
            <MudItem xs="12" sm="3"><MudText Typo="Typo.subtitle2" Class="mb-1">Zip Code</MudText><MudTextField @bind-Value="_model.AddressZipCode" ReadOnly="ReadOnly" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
            <MudItem xs="12" sm="3"><MudText Typo="Typo.subtitle2" Class="mb-1">Country</MudText><MudTextField @bind-Value="_model.AddressCountry" ReadOnly="ReadOnly" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>

            <MudItem xs="12" Class="mt-4"><MudText Typo="Typo.button" Color="Color.Primary" Style="font-weight: 800;">Contact & Communication</MudText><MudDivider /></MudItem>
            
            <MudItem xs="12" sm="4"><MudText Typo="Typo.subtitle2" Class="mb-1">Phone Number</MudText><MudTextField @bind-Value="_model.PhoneNumber" ReadOnly="ReadOnly" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
            <MudItem xs="12" sm="4"><MudText Typo="Typo.subtitle2" Class="mb-1">Fax Number</MudText><MudTextField @bind-Value="_model.FaxNumber" ReadOnly="ReadOnly" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
            <MudItem xs="12" sm="4"><MudText Typo="Typo.subtitle2" Class="mb-1">Mobile Number</MudText><MudTextField @bind-Value="_model.MobileNumber" ReadOnly="ReadOnly" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
            <MudItem xs="12" sm="6"><MudText Typo="Typo.subtitle2" Class="mb-1">Email Address</MudText><MudTextField @bind-Value="_model.Email" ReadOnly="ReadOnly" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
            <MudItem xs="12" sm="6"><MudText Typo="Typo.subtitle2" Class="mb-1">Website</MudText><MudTextField @bind-Value="_model.Website" ReadOnly="ReadOnly" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>

            <MudItem xs="12" Class="mt-4"><MudText Typo="Typo.button" Color="Color.Primary" Style="font-weight: 800;">Digital Presence</MudText><MudDivider /></MudItem>
            
            <MudItem xs="12" sm="4"><MudText Typo="Typo.subtitle2" Class="mb-1">WhatsApp</MudText><MudTextField @bind-Value="_model.WhatsApp" ReadOnly="ReadOnly" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
            <MudItem xs="12" sm="4"><MudText Typo="Typo.subtitle2" Class="mb-1">LinkedIn</MudText><MudTextField @bind-Value="_model.LinkedIn" ReadOnly="ReadOnly" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
            <MudItem xs="12" sm="4"><MudText Typo="Typo.subtitle2" Class="mb-1">Facebook</MudText><MudTextField @bind-Value="_model.Facebook" ReadOnly="ReadOnly" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
            <MudItem xs="12" sm="6"><MudText Typo="Typo.subtitle2" Class="mb-1">Twitter</MudText><MudTextField @bind-Value="_model.Twitter" ReadOnly="ReadOnly" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
            <MudItem xs="12" sm="6"><MudText Typo="Typo.subtitle2" Class="mb-1">Instagram</MudText><MudTextField @bind-Value="_model.Instagram" ReadOnly="ReadOnly" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>

            <MudItem xs="12" Class="mt-4"><MudText Typo="Typo.button" Color="Color.Primary" Style="font-weight: 800;">Audit History</MudText><MudDivider Class="mt-2 mb-4" /></MudItem>
            <MudItem xs="12" sm="6"><MudText Typo="Typo.subtitle2">Created At</MudText><MudText Typo="Typo.body2" Style="font-weight: 600;">@DateTimeExtensions.ToString(_model.CreatedAt)</MudText></MudItem>
            <MudItem xs="12" sm="6"><MudText Typo="Typo.subtitle2">Created By</MudText><MudText Typo="Typo.body2" Style="font-weight: 600;">@(!string.IsNullOrEmpty(_model.CreatedBy) ? _model.CreatedBy : "System")</MudText></MudItem>
            <MudItem xs="12" sm="6"><MudText Typo="Typo.subtitle2">Last Updated At</MudText><MudText Typo="Typo.body2" Style="font-weight: 600;">@DateTimeExtensions.ToString(_model.UpdatedAt)</MudText></MudItem>
            <MudItem xs="12" sm="6"><MudText Typo="Typo.subtitle2">Last Updated By</MudText><MudText Typo="Typo.body2" Style="font-weight: 600;">@(!string.IsNullOrEmpty(_model.UpdatedBy) ? _model.UpdatedBy : "System")</MudText></MudItem>
        </MudGrid>

        <div class="d-flex justify-end gap-2 mt-10">
            <MudButton OnClick="() => OnCancel.InvokeAsync()" Variant="Variant.Outlined" Style="border-radius: 4px; border: 1px solid #e0e0e0; text-transform: none; font-weight: 700;">@(ReadOnly ? "Back to List" : "Cancel")</MudButton>
            @if (!ReadOnly)
            {
                <MudButton Color="Color.Primary" Variant="Variant.Filled" OnClick="Submit" Disabled="_processing" Style="border-radius: 4px; text-transform: none; font-weight: 700;">
                    @if (_processing)
                    {
                        <MudProgressCircular Class="ms-n1" Size="Size.Small" Indeterminate="true" />
                        <MudText Class="ms-2" Typo="Typo.button" Style="text-transform: none !important;">Updating...</MudText>
                    }
                    else
                    {
                        <MudText>Save Changes</MudText>
                    }
                </MudButton>
            }
        </div>
    </MudForm>
</MudPaper>

@code {
    [Parameter] public UpdateLeadContactRequest Data { get; set; } = new();
    [Parameter] public bool ReadOnly { get; set; } = false;
    [Parameter] public EventCallback OnCancel { get; set; }
    [Parameter] public EventCallback OnSuccess { get; set; }

    private MudForm _form = default!;
    private UpdateLeadContactValidator _validator = new();
    private UpdateLeadContactRequest _model = new();
    private bool _processing = false;
    private LeadContactLookupResponse _lookup = new();

    protected override async Task OnInitializedAsync()
    {
        var res = await LeadContactService.GetLeadContactLookupAsync();
        if (res != null && res.IsSuccess)
        {
            _lookup = res.Value ?? new();
        }

        _model = new UpdateLeadContactRequest
        {
            Id = Data.Id,
            LeadId = Data.LeadId,
            FullName = Data.FullName,
            Description = Data.Description,
            AddressStreet = Data.AddressStreet,
            AddressCity = Data.AddressCity,
            AddressState = Data.AddressState,
            AddressZipCode = Data.AddressZipCode,
            AddressCountry = Data.AddressCountry,
            PhoneNumber = Data.PhoneNumber,
            FaxNumber = Data.FaxNumber,
            MobileNumber = Data.MobileNumber,
            Email = Data.Email,
            Website = Data.Website,
            WhatsApp = Data.WhatsApp,
            LinkedIn = Data.LinkedIn,
            Facebook = Data.Facebook,
            Twitter = Data.Twitter,
            Instagram = Data.Instagram,
            CreatedAt = Data.CreatedAt,
            CreatedBy = Data.CreatedBy,
            UpdatedAt = Data.UpdatedAt,
            UpdatedBy = Data.UpdatedBy
        };
        
        StateHasChanged();
    }

    private async Task Submit()
    {
        await _form.Validate();
        if (!_form.IsValid) return;
        _processing = true;
        try
        {
            var res = await LeadContactService.UpdateLeadContactAsync(_model);
            await Task.Delay(500);
            if (res != null && res.IsSuccess)
            {
                Snackbar.Add("Updated successfully", Severity.Success);
                await OnSuccess.InvokeAsync();
            }
        }
        finally { _processing = false; }
    }
}