@page "/salesdocuments/{Id:int}"
@using Domain.Constants
@using Domain.Dtos.Sales
@using phronCare.UIBlazor.Services.Sales.SalesDocuments
@inject NavigationManager Navigation
@inject ISalesDocumentService SalesDocumentService
@inject IToastService toastService

<div class="container mt-4" style="zoom:.8;">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h3 class="mb-0">Sales Document</h3>
        <div class="d-flex flex-wrap gap-2">
            @if (Document?.Status == (int)SalesDocumentStatus.Draft)
            {
                <button type="button" class="btn btn-success rounded-pill" @onclick="ReviewDraft">
                    <i class="fas fa-clipboard-check me-1"></i> Revisar draft
                </button>
            }
            <button type="button" class="btn btn-secondary rounded-pill" @onclick="BackToList">
                <i class="fas fa-arrow-left me-1"></i> Volver
            </button>
        </div>
    </div>

    @if (IsLoading)
    {
        <div class="card shadow-sm"><div class="card-body text-center text-muted py-4">Cargando...</div></div>
    }
    else if (Document is null)
    {
        <div class="alert alert-warning">No se pudo cargar el Sales Document.</div>
    }
    else
    {
        <div class="card shadow-sm mb-3">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0">@(string.IsNullOrWhiteSpace(Document.InternalDocumentNumber) ? $"#{Document.Id}" : Document.InternalDocumentNumber)</h5>
                <span class="badge @GetStatusBadge(Document.Status)">@GetStatusLabel(Document.Status)</span>
            </div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-3">
                        <label class="form-label fw-semibold mb-1">Fecha</label>
                        <div class="form-control bg-white">@FormatDate(Document.IssueDate)</div>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label fw-semibold mb-1">Tipo</label>
                        <div class="form-control bg-white">@GetDocumentTypeLabel(Document.DocumentType)</div>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label fw-semibold mb-1">Origen</label>
                        <div class="form-control bg-white">@GetOriginSummary()</div>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label fw-semibold mb-1">Total</label>
                        <div class="form-control bg-white">@Document.Currency @Document.TotalAmount.ToString("N2")</div>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label fw-semibold mb-1">Cliente</label>
                        <div class="form-control bg-white">@Document.CustomerName</div>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label fw-semibold mb-1">Facturar a</label>
                        <div class="form-control bg-white">@Document.BillToCustomerName</div>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label fw-semibold mb-1">Presupuesto</label>
                        <div class="form-control bg-white">@(Document.QuoteId?.ToString() ?? "—")</div>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label fw-semibold mb-1">Neto</label>
                        <div class="form-control bg-white">@Document.NetAmount.ToString("N2")</div>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label fw-semibold mb-1">Impuestos</label>
                        <div class="form-control bg-white">@Document.TaxAmount.ToString("N2")</div>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label fw-semibold mb-1">Cotización</label>
                        <div class="form-control bg-white">@Document.ExchangeRate.ToString("N4")</div>
                    </div>
                    <div class="col-12">
                        <label class="form-label fw-semibold mb-1">Observaciones</label>
                        <div class="form-control bg-white">@(string.IsNullOrWhiteSpace(Document.Observations) ? "—" : Document.Observations)</div>
                    </div>
                </div>
            </div>
        </div>

        <div class="card shadow-sm mb-3">
            <div class="card-header"><h5 class="mb-0">Coverage</h5></div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-sm table-bordered mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>Tipo</th>
                                <th>Presupuesto</th>
                                <th>Quote detail</th>
                                <th>Porcentaje</th>
                                <th>Importe</th>
                                <th>Desde</th>
                                <th>Hasta</th>
                                <th>Notas</th>
                            </tr>
                        </thead>
                        <tbody>
                            @if (Document.Coverage.Any())
                            {
                                @foreach (var coverage in Document.Coverage)
                                {
                                    <tr class="text-center">
                                        <td>@GetCoverageTypeLabel(coverage.CoverageType)</td>
                                        <td>@coverage.QuoteId</td>
                                        <td>@(coverage.QuoteDetailId?.ToString() ?? "—")</td>
                                        <td>@(coverage.CoveragePercentage?.ToString("N2") ?? "—")</td>
                                        <td>@(coverage.CoverageAmount?.ToString("N2") ?? "—")</td>
                                        <td>@FormatDate(coverage.PeriodFrom)</td>
                                        <td>@FormatDate(coverage.PeriodTo)</td>
                                        <td>@(string.IsNullOrWhiteSpace(coverage.Notes) ? "—" : coverage.Notes)</td>
                                    </tr>
                                }
                            }
                            else
                            {
                                <tr><td colspan="8" class="text-center text-muted py-4">Sin coverage informado.</td></tr>
                            }
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="card shadow-sm">
            <div class="card-header"><h5 class="mb-0">Detalles</h5></div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-sm table-bordered mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>#</th>
                                <th>Origen</th>
                                <th>Descripción</th>
                                <th>Cantidad</th>
                                <th>Unitario</th>
                                <th>Neto</th>
                                <th>Impuesto</th>
                                <th>Total</th>
                            </tr>
                        </thead>
                        <tbody>
                            @if (Document.Details.Any())
                            {
                                @foreach (var item in Document.Details.OrderBy(x => x.LineNumber))
                                {
                                    <tr>
                                        <td class="text-center">@item.LineNumber</td>
                                        <td class="text-center">@GetOriginTypeLabel(item.OriginType)</td>
                                        <td>@item.Description</td>
                                        <td class="text-end">@item.Quantity.ToString("N2")</td>
                                        <td class="text-end">@item.UnitPrice.ToString("N2")</td>
                                        <td class="text-end">@item.NetAmount.ToString("N2")</td>
                                        <td class="text-end">@item.TaxAmount.ToString("N2")</td>
                                        <td class="text-end">@item.TotalAmount.ToString("N2")</td>
                                    </tr>
                                }
                            }
                            else
                            {
                                <tr><td colspan="8" class="text-center text-muted py-4">Sin detalles.</td></tr>
                            }
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    }
</div>

@code {
    [Parameter] public int Id { get; set; }

    private SalesDocumentDto? Document;
    private bool IsLoading;

    protected override async Task OnInitializedAsync()
    {
        await LoadAsync();
    }

    private async Task LoadAsync()
    {
        try
        {
            IsLoading = true;
            Document = await SalesDocumentService.GetByIdAsync(Id);
        }
        catch (Exception ex)
        {
            toastService.ShowError(ex.Message);
        }
        finally
        {
            IsLoading = false;
        }
    }

    private void BackToList() => Navigation.NavigateTo("/salesdocuments");
    private void ReviewDraft() => Navigation.NavigateTo($"/salesdocuments/{Id}/review");

    private string GetOriginSummary()
    {
        if (Document?.Details?.Any() != true)
            return "—";

        var origins = Document.Details
            .Select(x => GetOriginTypeLabel(x.OriginType))
            .Distinct()
            .ToList();

        return origins.Count == 1 ? origins[0] : string.Join(" / ", origins);
    }

    private static string FormatDate(DateTime? value) => value.HasValue ? value.Value.ToString("dd/MM/yyyy") : "—";

    private static string GetDocumentTypeLabel(int value) => Enum.IsDefined(typeof(SalesDocumentType), value)
        ? ((SalesDocumentType)value) switch
        {
            SalesDocumentType.Invoice => "Factura",
            SalesDocumentType.DebitNote => "Nota de débito",
            SalesDocumentType.CreditNote => "Nota de crédito",
            SalesDocumentType.CreditInvoice => "Factura crédito",
            SalesDocumentType.CreditDebitNote => "N/D crédito",
            SalesDocumentType.CreditCreditNote => "N/C crédito",
            _ => value.ToString()
        }
        : value.ToString();

    private static string GetStatusLabel(int value) => Enum.IsDefined(typeof(SalesDocumentStatus), value)
        ? ((SalesDocumentStatus)value) switch
        {
            SalesDocumentStatus.Draft => "Borrador",
            SalesDocumentStatus.Validated => "Validado",
            SalesDocumentStatus.Issued => "Emitido",
            SalesDocumentStatus.Cancelled => "Anulado",
            _ => value.ToString()
        }
        : value.ToString();

    private static string GetCoverageTypeLabel(int value) => Enum.IsDefined(typeof(SalesDocumentCoverageType), value)
        ? ((SalesDocumentCoverageType)value) switch
        {
            SalesDocumentCoverageType.Direct => "Directa",
            SalesDocumentCoverageType.Capita => "Cápita",
            SalesDocumentCoverageType.Adjustment => "Ajuste",
            SalesDocumentCoverageType.Manual => "Manual",
            _ => value.ToString()
        }
        : value.ToString();

    private static string GetOriginTypeLabel(string value) => value switch
    {
        "MANUAL" => "Manual",
        "QUOTE" => "Presupuesto",
        "ADJUSTMENT" => "Ajuste",
        "CAPITA" => "Cápita",
        "DELIVERY_NOTE" => "Remito",
        _ => string.IsNullOrWhiteSpace(value) ? "—" : value
    };

    private static string GetStatusBadge(int value) => value switch
    {
        (int)SalesDocumentStatus.Draft => "bg-secondary text-white",
        (int)SalesDocumentStatus.Validated => "bg-info text-dark",
        (int)SalesDocumentStatus.Issued => "bg-primary text-white",
        (int)SalesDocumentStatus.Cancelled => "bg-danger text-white",
        _ => "bg-light text-dark"
    };
}
