using FrontOffice.Main.Utilities;
using Microsoft.AspNetCore.Components;

namespace FrontOffice.Main.Pages.Package;

public partial class Packages : ComponentBase
{
    [Inject] private PackageService PackageService { get; set; } = default!;

    private List<PackageDto> _packages = new();
    private UserPackageStatusDto? _userStatus;
    private bool _isLoading = true;

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

    private async Task LoadDataAsync()
    {
        _isLoading = true;

        try
        {
            // Load packages
            _packages = await PackageService.GetAllPackagesAsync();

            // Load user status (if authenticated)
            _userStatus = await PackageService.GetUserPackageStatusAsync();
        }
        catch (Exception ex)
        {
#if DEBUG
            Console.WriteLine($"Error loading packages: {ex.Message}");
#endif
        }
        finally
        {
            _isLoading = false;
        }
    }

    private void ViewPackageDetails(long packageId)
    {
        Navigation.NavigateTo(RouteConstants.Package.Detail + packageId);
    }

    private static string TruncateDescription(string description, int maxLength = 100)
    {
        if (string.IsNullOrEmpty(description)) return string.Empty;
        if (description.Length <= maxLength) return description;
        return description[..maxLength] + "...";
    }
}
