@using Indotalent.Features.Pipeline.Lead
@using Indotalent.Features.Pipeline.Lead.Cqrs
@using Indotalent.Shared.Utils
@using MudBlazor
@inject LeadService LeadService
@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;">Add New Lead</MudText>
        <MudText Typo="Typo.body2" Style="color: #64748b;">Enter full lead profile and company details.</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"><MudText Typo="Typo.subtitle2" Class="mb-1">Lead Title</MudText><MudTextField @bind-Value="_model.Title" For="@(() => _model.Title)" Validation="@(_validator.ValidateValue())" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
            <MudItem xs="12"><MudText Typo="Typo.subtitle2" Class="mb-1">Description</MudText><MudTextField @bind-Value="_model.Description" Variant="Variant.Outlined" Margin="Margin.Dense" Lines="3" FullWidth="true" /></MudItem>
            
            <MudItem xs="12" sm="6"><MudText Typo="Typo.subtitle2" Class="mb-1">Campaign</MudText><MudSelect T="string" @bind-Value="_model.CampaignId" For="@(() => _model.CampaignId)" Validation="@(_validator.ValidateValue())" Variant="Variant.Outlined" Margin="Margin.Dense" Dense="true" FullWidth="true" AnchorOrigin="Origin.BottomCenter" TransformOrigin="Origin.TopCenter">@foreach (var item in _lookup.Campaigns) {
                <MudSelectItem Value="@item.Id">@item.Name</MudSelectItem>
            }
        </MudSelect></MudItem>
        <MudItem xs="12" sm="6"><MudText Typo="Typo.subtitle2" Class="mb-1">Sales Team</MudText><MudSelect T="string" @bind-Value="_model.SalesTeamId" For="@(() => _model.SalesTeamId)" Validation="@(_validator.ValidateValue())" Variant="Variant.Outlined" Margin="Margin.Dense" Dense="true" FullWidth="true" AnchorOrigin="Origin.BottomCenter" TransformOrigin="Origin.TopCenter">@foreach (var item in _lookup.SalesTeams) {
            <MudSelectItem Value="@item.Id">@item.Name</MudSelectItem>
        }
    </MudSelect></MudItem>

    <MudItem xs="12" Class="mt-4"><MudText Typo="Typo.button" Color="Color.Primary" Style="font-weight: 800;">Company Details</MudText><MudDivider /></MudItem>
    <MudItem xs="12" sm="6"><MudText Typo="Typo.subtitle2" Class="mb-1">Company Name</MudText><MudTextField @bind-Value="_model.CompanyName" For="@(() => _model.CompanyName)" Validation="@(_validator.ValidateValue())" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
    <MudItem xs="12" sm="6"><MudText Typo="Typo.subtitle2" Class="mb-1">Company Description</MudText><MudTextField @bind-Value="_model.CompanyDescription" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
    <MudItem xs="12"><MudText Typo="Typo.subtitle2" Class="mb-1">Street Address</MudText><MudTextField @bind-Value="_model.CompanyAddressStreet" 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.CompanyAddressCity" 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.CompanyAddressState" 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.CompanyAddressZipCode" 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.CompanyAddressCountry" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>

    <MudItem xs="12" sm="4"><MudText Typo="Typo.subtitle2" Class="mb-1">Phone</MudText><MudTextField @bind-Value="_model.CompanyPhoneNumber" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
    <MudItem xs="12" sm="4"><MudText Typo="Typo.subtitle2" Class="mb-1">Fax</MudText><MudTextField @bind-Value="_model.CompanyFaxNumber" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
    <MudItem xs="12" sm="4"><MudText Typo="Typo.subtitle2" Class="mb-1">Email</MudText><MudTextField @bind-Value="_model.CompanyEmail" 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">Website</MudText><MudTextField @bind-Value="_model.CompanyWebsite" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
    <MudItem xs="12" sm="4"><MudText Typo="Typo.subtitle2" Class="mb-1">WhatsApp</MudText><MudTextField @bind-Value="_model.CompanyWhatsApp" 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.CompanyLinkedIn" 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.CompanyFacebook" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
    <MudItem xs="12" sm="4"><MudText Typo="Typo.subtitle2" Class="mb-1">Instagram</MudText><MudTextField @bind-Value="_model.CompanyInstagram" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
    <MudItem xs="12" sm="4"><MudText Typo="Typo.subtitle2" Class="mb-1">Twitter</MudText><MudTextField @bind-Value="_model.CompanyTwitter" 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;">Dates & Targeted Amount</MudText><MudDivider /></MudItem>
    <MudItem xs="12" sm="4"><MudText Typo="Typo.subtitle2" Class="mb-1">Prospecting Date</MudText><MudDatePicker @bind-Date="_model.DateProspecting" Variant="Variant.Outlined" Margin="Margin.Dense" /></MudItem>
    <MudItem xs="12" sm="4"><MudText Typo="Typo.subtitle2" Class="mb-1">Closing Estimation</MudText><MudDatePicker @bind-Date="_model.DateClosingEstimation" Variant="Variant.Outlined" Margin="Margin.Dense" /></MudItem>
    <MudItem xs="12" sm="4"><MudText Typo="Typo.subtitle2" Class="mb-1">Closing Actual</MudText><MudDatePicker @bind-Date="_model.DateClosingActual" Variant="Variant.Outlined" Margin="Margin.Dense" /></MudItem>

    <MudItem xs="12" sm="6"><MudText Typo="Typo.subtitle2" Class="mb-1">Amount Targeted</MudText><MudNumericField @bind-Value="_model.AmountTargeted" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
    <MudItem xs="12" sm="6"><MudText Typo="Typo.subtitle2" Class="mb-1">Amount Closed</MudText><MudNumericField @bind-Value="_model.AmountClosed" 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;">BANT Scoring</MudText><MudDivider /></MudItem>
    <MudItem xs="12" sm="3"><MudText Typo="Typo.subtitle2" Class="mb-1">Budget Score</MudText><MudNumericField @bind-Value="_model.BudgetScore" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
    <MudItem xs="12" sm="3"><MudText Typo="Typo.subtitle2" Class="mb-1">Authority Score</MudText><MudNumericField @bind-Value="_model.AuthorityScore" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
    <MudItem xs="12" sm="3"><MudText Typo="Typo.subtitle2" Class="mb-1">Need Score</MudText><MudNumericField @bind-Value="_model.NeedScore" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" /></MudItem>
    <MudItem xs="12" sm="3"><MudText Typo="Typo.subtitle2" Class="mb-1">Timeline Score</MudText><MudNumericField @bind-Value="_model.TimelineScore" 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;">Pipeline & Closing</MudText><MudDivider /></MudItem>
    <MudItem xs="12" sm="6"><MudText Typo="Typo.subtitle2" Class="mb-1">Pipeline Stage</MudText><MudSelect T="Indotalent.Data.Enums.PipelineStage" @bind-Value="_model.PipelineStage" Variant="Variant.Outlined" Margin="Margin.Dense" Dense="true" FullWidth="true" AnchorOrigin="Origin.BottomCenter" TransformOrigin="Origin.TopCenter">@foreach (var item in _lookup.PipelineStages) {
        <MudSelectItem Value="@((Indotalent.Data.Enums.PipelineStage)item.Value)">@item.Name</MudSelectItem>
    }
</MudSelect></MudItem>
<MudItem xs="12" sm="6"><MudText Typo="Typo.subtitle2" Class="mb-1">Closing Status</MudText><MudSelect T="Indotalent.Data.Enums.ClosingStatus" @bind-Value="_model.ClosingStatus" Variant="Variant.Outlined" Margin="Margin.Dense" Dense="true" FullWidth="true" AnchorOrigin="Origin.BottomCenter" TransformOrigin="Origin.TopCenter">@foreach (var item in _lookup.ClosingStatuses) {
    <MudSelectItem Value="@((Indotalent.Data.Enums.ClosingStatus)item.Value)">@item.Name</MudSelectItem>
}
</MudSelect></MudItem>
<MudItem xs="12"><MudText Typo="Typo.subtitle2" Class="mb-1">Closing Note</MudText><MudTextField @bind-Value="_model.ClosingNote" Variant="Variant.Outlined" Margin="Margin.Dense" Lines="2" FullWidth="true" /></MudItem>
</MudGrid>
<div class="d-flex justify-end gap-2 mt-8">
    <MudButton OnClick="() => OnCancel.InvokeAsync()" Variant="Variant.Outlined" Style="border-radius: 4px; border: 1px solid #e0e0e0; text-transform: none; font-weight: 700;">Cancel</MudButton>
    <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;">Processing...</MudText>
        }
        else
        {

            <MudText>Create Lead</MudText>
        }
    </MudButton>
</div>
</MudForm>
</MudPaper>

@code {
    [Parameter] public EventCallback OnCancel { get; set; }
    [Parameter] public EventCallback OnSuccess { get; set; }
    private MudForm _form = default!;
    private CreateLeadValidator _validator = new();
    private CreateLeadRequest _model = new();
    private LeadLookupResponse _lookup = new();
    private bool _processing = false;

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

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