@using Indotalent.Features.Setting.Company
@using Indotalent.Features.Setting.Company.Cqrs
@using Indotalent.Features.Setting.Currency
@using Indotalent.Features.Setting.Currency.Cqrs
@using Indotalent.Shared.Utils
@using MudBlazor
@inject CompanyService CompanyService
@inject CurrencyService CurrencyService
@inject ISnackbar Snackbar

<MudPaper Elevation="0" Square="true" Class="pa-6 mb-3 d-flex align-center justify-space-between" Style="border: 1px solid #DCEBFA;">
    <div class="d-flex align-center gap-4">
        <MudIconButton Icon="@Icons.Material.Filled.ArrowBack" OnClick="() => OnCancel.InvokeAsync()" />
        <div>
            <MudText Typo="Typo.h5" Style="font-weight: 900; color: #1a1a1a;">Add New Company</MudText>
            <MudText Typo="Typo.body2" Style="color: #64748b;">Configure organization identity and legal details.</MudText>
        </div>
    </div>
</MudPaper>

<MudPaper Elevation="0" Outlined="true" Class="pa-8" Style="border-radius: 0px; border: 1px solid #DCEBFA;">
    <MudForm @ref="_form" Model="_model">
        <MudStack Spacing="8">
            <MudGrid>
                <MudItem xs="12" md="4">
                    <MudText Typo="Typo.h6" Class="fw-bold">Company Profile</MudText>
                    <MudText Typo="Typo.body2">Basic identity and branding of your organization.</MudText>
                </MudItem>
                <MudItem xs="12" md="8">
                    <MudStack Spacing="4">
                        <MudGrid Spacing="2">
                            <MudItem xs="12" sm="8">
                                <div class="d-flex flex-column justify-end h-100">
                                    <MudText Typo="Typo.subtitle2" Class="mb-1">Company Name</MudText>
                                    <MudTextField @bind-Value="_model.Name"
                                                  For="@(() => _model.Name)"
                                                  Validation="@(_validator.ValidateValue())"
                                                  Variant="Variant.Outlined"
                                                  FullWidth="true"
                                                  Margin="Margin.Dense" />
                                </div>
                            </MudItem>
                            <MudItem xs="12" sm="4">
                                <div class="d-flex flex-column justify-end h-100">
                                    <MudText Typo="Typo.subtitle2" Class="mb-1">Default Currency</MudText>
                                    <MudSelect @bind-Value="_model.CurrencyId"
                                               For="@(() => _model.CurrencyId)"
                                               Validation="@(_validator.ValidateValue())"
                                               Variant="Variant.Outlined"
                                               FullWidth="true"
                                               Margin="Margin.Dense"
                                               Dense="true"
                                               AnchorOrigin="Origin.BottomCenter"
                                               TransformOrigin="Origin.TopCenter">
                                        @foreach (var curr in _currencies)
                                        {
                                            <MudSelectItem Value="@curr.Id">
                                                <div class="d-flex align-center gap-2">
                                                    <MudText Typo="Typo.body2" Style="font-weight: 700;">@curr.Code</MudText>
                                                    <MudText Typo="Typo.caption" Style="color: #64748b;">(@curr.Symbol)</MudText>
                                                </div>
                                            </MudSelectItem>
                                        }
                                    </MudSelect>
                                </div>
                            </MudItem>
                        </MudGrid>
                        <div>
                            <MudText Typo="Typo.subtitle2" Class="mb-1">Company Description</MudText>
                            <MudTextField @bind-Value="_model.Description" Variant="Variant.Outlined" Lines="3" FullWidth="true" Margin="Margin.Dense" />
                        </div>
                        <MudGrid Spacing="2">
                            <MudItem xs="12">
                                <MudText Typo="Typo.subtitle2" Class="mb-1">Set as Default Company</MudText>
                                <MudSwitch @bind-Value="_model.IsDefault"
                                           Color="Color.Primary"
                                           Label="@(_model.IsDefault ? "This is the primary organization" : "Set this as the primary organization")" />
                            </MudItem>
                        </MudGrid>
                        <MudGrid Spacing="2">
                            <MudItem xs="12" sm="6">
                                <MudText Typo="Typo.subtitle2" Class="mb-1">Tax ID (NPWP)</MudText>
                                <MudTextField @bind-Value="_model.TaxIdentification" Variant="Variant.Outlined" FullWidth="true" Margin="Margin.Dense" />
                            </MudItem>
                            <MudItem xs="12" sm="6">
                                <MudText Typo="Typo.subtitle2" Class="mb-1">Business License (NIB)</MudText>
                                <MudTextField @bind-Value="_model.BusinessLicense" Variant="Variant.Outlined" FullWidth="true" Margin="Margin.Dense" />
                            </MudItem>
                        </MudGrid>
                    </MudStack>
                </MudItem>
            </MudGrid>

            <MudDivider />

            <MudGrid>
                <MudItem xs="12" md="4">
                    <MudText Typo="Typo.h6" Class="fw-bold">Head Office Address</MudText>
                    <MudText Typo="Typo.body2">Main office location and contact details.</MudText>
                </MudItem>
                <MudItem xs="12" md="8">
                    <MudStack Spacing="4">
                        <div>
                            <MudText Typo="Typo.subtitle2" Class="mb-1">Street Address</MudText>
                            <MudTextField @bind-Value="_model.StreetAddress" Variant="Variant.Outlined" FullWidth="true" Margin="Margin.Dense" />
                        </div>
                        <MudGrid Spacing="2">
                            <MudItem xs="12" sm="4">
                                <MudText Typo="Typo.subtitle2" Class="mb-1">City</MudText>
                                <MudTextField @bind-Value="_model.City" Variant="Variant.Outlined" FullWidth="true" Margin="Margin.Dense" />
                            </MudItem>
                            <MudItem xs="12" sm="4">
                                <MudText Typo="Typo.subtitle2" Class="mb-1">State/Province</MudText>
                                <MudTextField @bind-Value="_model.StateProvince" Variant="Variant.Outlined" FullWidth="true" Margin="Margin.Dense" />
                            </MudItem>
                            <MudItem xs="12" sm="4">
                                <MudText Typo="Typo.subtitle2" Class="mb-1">ZIP Code</MudText>
                                <MudTextField @bind-Value="_model.ZipCode" Variant="Variant.Outlined" FullWidth="true" Margin="Margin.Dense" />
                            </MudItem>
                        </MudGrid>
                        <MudGrid Spacing="2">
                            <MudItem xs="12" sm="6">
                                <MudText Typo="Typo.subtitle2" Class="mb-1">Phone</MudText>
                                <MudTextField @bind-Value="_model.Phone" Variant="Variant.Outlined" FullWidth="true" Margin="Margin.Dense" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Phone" />
                            </MudItem>
                            <MudItem xs="12" sm="6">
                                <MudText Typo="Typo.subtitle2" Class="mb-1">Email</MudText>
                                <MudTextField @bind-Value="_model.Email" For="@(() => _model.Email)" Validation="@(_validator.ValidateValue())" Variant="Variant.Outlined" FullWidth="true" Margin="Margin.Dense" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Email" />
                            </MudItem>
                        </MudGrid>
                    </MudStack>
                </MudItem>
            </MudGrid>

            <MudDivider />

            <MudGrid>
                <MudItem xs="12" md="4">
                    <MudText Typo="Typo.h6" Class="fw-bold">Social Media Presence</MudText>
                    <MudText Typo="Typo.body2">External links for recruitment branding.</MudText>
                </MudItem>
                <MudItem xs="12" md="8">
                    <MudStack Spacing="4">
                        <div>
                            <MudText Typo="Typo.subtitle2" Class="mb-1">LinkedIn</MudText>
                            <MudTextField @bind-Value="_model.SocialMediaLinkedIn" Variant="Variant.Outlined" Margin="Margin.Dense" Adornment="Adornment.Start" AdornmentIcon="@Icons.Custom.Brands.LinkedIn" FullWidth="true" />
                        </div>
                        <div>
                            <MudText Typo="Typo.subtitle2" Class="mb-1">X (Twitter)</MudText>
                            <MudTextField @bind-Value="_model.SocialMediaX" Variant="Variant.Outlined" Margin="Margin.Dense" Adornment="Adornment.Start" AdornmentIcon="@Icons.Custom.Brands.Twitter" FullWidth="true" />
                        </div>
                        <div>
                            <MudText Typo="Typo.subtitle2" Class="mb-1">Facebook</MudText>
                            <MudTextField @bind-Value="_model.SocialMediaFacebook" Variant="Variant.Outlined" Margin="Margin.Dense" Adornment="Adornment.Start" AdornmentIcon="@Icons.Custom.Brands.Facebook" FullWidth="true" />
                        </div>
                        <div>
                            <MudText Typo="Typo.subtitle2" Class="mb-1">Instagram</MudText>
                            <MudTextField @bind-Value="_model.SocialMediaInstagram" Variant="Variant.Outlined" Margin="Margin.Dense" Adornment="Adornment.Start" AdornmentIcon="@Icons.Custom.Brands.Instagram" FullWidth="true" />
                        </div>
                        <div>
                            <MudText Typo="Typo.subtitle2" Class="mb-1">TikTok</MudText>
                            <MudTextField @bind-Value="_model.SocialMediaTikTok" Variant="Variant.Outlined" Margin="Margin.Dense" Adornment="Adornment.Start" AdornmentIcon="@Icons.Custom.Brands.TikTok" FullWidth="true" />
                        </div>
                    </MudStack>
                </MudItem>
            </MudGrid>

            <MudDivider />

            <MudGrid>
                <MudItem xs="12" md="4">
                    <MudText Typo="Typo.h6" Class="fw-bold">Other Information</MudText>
                    <MudText Typo="Typo.body2">Additional company metadata.</MudText>
                </MudItem>
                <MudItem xs="12" md="8">
                    <MudStack Spacing="4">
                        <div>
                            <MudText Typo="Typo.subtitle2" Class="mb-1">Other Information 1</MudText>
                            <MudTextField @bind-Value="_model.OtherInformation1" Variant="Variant.Outlined" FullWidth="true" Margin="Margin.Dense" />
                        </div>
                        <div>
                            <MudText Typo="Typo.subtitle2" Class="mb-1">Other Information 2</MudText>
                            <MudTextField @bind-Value="_model.OtherInformation2" Variant="Variant.Outlined" FullWidth="true" Margin="Margin.Dense" />
                        </div>
                        <div>
                            <MudText Typo="Typo.subtitle2" Class="mb-1">Other Information 3</MudText>
                            <MudTextField @bind-Value="_model.OtherInformation3" Variant="Variant.Outlined" FullWidth="true" Margin="Margin.Dense" />
                        </div>
                    </MudStack>
                </MudItem>
            </MudGrid>

            <div class="d-flex justify-end gap-2 mt-8">
                <MudButton OnClick="() => OnCancel.InvokeAsync()" Variant="Variant.Outlined" Disabled="_processing" 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 Company</MudText>
                    }
                </MudButton>
            </div>
        </MudStack>
    </MudForm>
</MudPaper>

@code {
    [Parameter] public EventCallback OnCancel { get; set; }
    [Parameter] public EventCallback OnSuccess { get; set; }

    private MudForm _form = default!;
    private CreateCompanyValidator _validator = new();
    private CreateCompanyRequest _model = new();
    private List<GetCurrencyListResponse> _currencies = new();
    private bool _processing = false;

    protected override async Task OnInitializedAsync()
    {
        var response = await CurrencyService.GetCurrencyListAsync();
        if (response != null && response.IsSuccess)
        {
            _currencies = response.Value ?? new();
        }
    }

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