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

namespace FrontOffice.Main.Pages.Club;

public partial class MembershipPage : ComponentBase
{
    [Inject] private ClubMembershipService ClubService { get; set; } = default!;
    [Inject] private ClubConfigurationService ConfigService { get; set; } = default!;

    private ClubMembershipDto? _membership;
    private ClubConfigDto? _clubConfig;
    private bool _isLoading = true;
    private bool _hasError = false;

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

    private async Task LoadDataAsync()
    {
        try
        {
            _isLoading = true;
            _hasError = false;
            
            // بارگذاری همزمان عضویت و تنظیمات
            var membershipTask = ClubService.GetMyMembershipAsync();
            var configTask = ConfigService.GetClubConfigurationAsync();
            
            await Task.WhenAll(membershipTask, configTask);
            
            _membership = await membershipTask;
            _clubConfig = await configTask;
        }
        catch (Exception ex)
        {
            _hasError = true;
            Snackbar.Add($"خطا در دریافت اطلاعات: {ex.Message}", Severity.Error);
        }
        finally
        {
            _isLoading = false;
        }
    }

    private async Task HandleActivationSuccess()
    {
        await LoadDataAsync();
        Snackbar.Add("عضویت باشگاه با موفقیت فعال شد!", Severity.Success);
    }

    private Color GetStatusColor()
    {
        if (_membership?.IsActive == true)
        {
            if (_membership.DaysRemaining.HasValue && _membership.DaysRemaining.Value <= 30)
                return Color.Warning;
            return Color.Success;
        }
        return Color.Error;
    }

    private string GetStatusText()
    {
        if (_membership?.IsActive == true)
        {
            if (_membership.DaysRemaining.HasValue && _membership.DaysRemaining.Value <= 30)
                return "نزدیک به انقضا";
            return "فعال";
        }
        return "غیرفعال";
    }
}
