namespace MidrandBookshop.Components.Pages;

public partial class Account
{
    private bool showAddForm = false;
    private AddressItem? editingAddress = null;
    private string newAddressName = "";
    private string newStreetAddress = "";
    private string newCity = "";
    private string newPostalCode = "";
    private bool isBilling, isShipping;

    private List<OrderItem> orderHistory = new()
    {
        new OrderItem { OrderId = "#MB-2026-9481", ProductId = "introduction-to-blazor", ProductTitle = "Introduction to Blazor WebAssembly Framework Development", OrderDate = new DateTime(2026, 5, 20), ShippingAddressName = "Home Address", Status = "Shipped", Total = 720.00 },
        new OrderItem { OrderId = "#MB-2026-8712", ProductId = "mastering-css-isolation", ProductTitle = "Mastering CSS Isolation in Modern .NET Web Applications Architecture", OrderDate = new DateTime(2026, 4, 14), ShippingAddressName = "Midrand Books Warehouse", Status = "Delivered", Total = 890.00 }
    };

    private List<AddressItem> savedAddresses = new()
    {
        new AddressItem { Id = 1, Name = "Home Address", Street = "12 Main Road", City = "Midrand", PostalCode = "1685", IsBilling = true, IsShipping = true, IsPrimary = true },
        new AddressItem { Id = 2, Name = "Corporate Office", Street = "45 Challink Street", City = "Halfway House", PostalCode = "1682", IsBilling = true, IsShipping = false, IsPrimary = false },
        new AddressItem { Id = 3, Name = "Midrand Books Warehouse", Street = "Unit 8, Corporate Park North", City = "Randjespark", PostalCode = "1683", IsBilling = false, IsShipping = true, IsPrimary = false }
    };

    private void TriggerLogout() => Navigation.NavigateTo("/logout", forceLoad: true);
    private void DownloadInvoice(string orderId) { /* Handle download sequence here */ }
    private void OpenAddForm() { editingAddress = null; showAddForm = true; }

    private void SaveAddress()
    {
        if (!string.IsNullOrWhiteSpace(newAddressName) && !string.IsNullOrWhiteSpace(newStreetAddress))
        {
            var nextId = savedAddresses.Any() ? savedAddresses.Max(a => a.Id) + 1 : 1;
            savedAddresses.Add(new AddressItem
            {
                Id = nextId,
                Name = newAddressName,
                Street = newStreetAddress,
                City = newCity,
                PostalCode = newPostalCode,
                IsBilling = isBilling,
                IsShipping = isShipping,
                IsPrimary = !savedAddresses.Any()
            });
            ResetAddForm();
        }
    }

    private void ResetAddForm() { newAddressName = ""; newStreetAddress = ""; newCity = ""; newPostalCode = ""; isBilling = isShipping = showAddForm = false; }
    private void StartEditing(AddressItem addr) { showAddForm = false; editingAddress = new AddressItem { Id = addr.Id, Name = addr.Name, Street = addr.Street, City = addr.City, PostalCode = addr.PostalCode, IsBilling = addr.IsBilling, IsShipping = addr.IsShipping, IsPrimary = addr.IsPrimary }; }
    private void CancelEditing() => editingAddress = null;

    private void UpdateAddress()
    {
        if (editingAddress != null)
        {
            var target = savedAddresses.FirstOrDefault(a => a.Id == editingAddress.Id);
            if (target != null) { target.Name = editingAddress.Name; target.Street = editingAddress.Street; target.City = editingAddress.City; target.PostalCode = editingAddress.PostalCode; target.IsBilling = editingAddress.IsBilling; target.IsShipping = editingAddress.IsShipping; }
            editingAddress = null;
        }
    }

    private void DeleteAddress(AddressItem addr) { if (editingAddress?.Id == addr.Id) editingAddress = null; savedAddresses.Remove(addr); if (addr.IsPrimary && savedAddresses.Any()) savedAddresses.First().IsPrimary = true; }

    private void SetPrimary(AddressItem target, ChangeEventArgs e)
    {
        var isChecked = (bool)(e.Value ?? false);
        if (isChecked) { foreach (var addr in savedAddresses) addr.IsPrimary = (addr.Id == target.Id); }
        else target.IsPrimary = false;
    }

    public class AddressItem { public int Id { get; set; } public string Name { get; set; } = ""; public string Street { get; set; } = ""; public string City { get; set; } = ""; public string PostalCode { get; set; } = ""; public bool IsBilling { get; set; } public bool IsShipping { get; set; } public bool IsPrimary { get; set; } }
    public class OrderItem { public string OrderId { get; set; } = ""; public string ProductId { get; set; } = ""; public string ProductTitle { get; set; } = ""; public DateTime OrderDate { get; set; } public string ShippingAddressName { get; set; } = ""; public string Status { get; set; } = ""; public double Total { get; set; } }
}
