using Microsoft.AspNetCore.Components;

using Front.Program.Models;
using Front.Program.Services;

namespace Front;

public partial class App : ComponentBase
{
    [Inject] private APIService API { get; set; } = default!;
    [Inject] private StorageService StorageService { get; set; } = default!;
    
    
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            var headerValue = await StorageService.GetItemAsync("Web-AM-Connect-Key");

            // 값 없으면 API 호출
            if (string.IsNullOrEmpty(headerValue))
            {
                var response = await API.GetJsonAsync<APIHeader, AppHeader>(
                    "/api/v1/in/app",
                    new AppHeader() 
                    {
                        type = "W",
                        specific = "Web_Connect",
                        project = "AcaMate"
                    });
                if (!string.IsNullOrEmpty(response.data.header))
                {
                    await StorageService.SetItemAsync("Web-AM-Connect-Key", response.data.header);
                }
            }
        }
    }
}