using Indotalent.ConfigBackEnd.Interfaces;
using Indotalent.ConfigFrontEnd.Common;
using Indotalent.Features.Setting.Company.Cqrs;
using Indotalent.Infrastructure.Authentication.Identity;
using Indotalent.Shared.Models;
using Microsoft.AspNetCore.Components;
using MudBlazor;
using RestSharp;

namespace Indotalent.Features.Setting.Company;

public class CompanyService : BaseService
{
    private readonly RestClient _client;

    public CompanyService(IHttpClientFactory cf, NavigationManager nav, ISnackbar snack, ICurrentUserService cus, TokenProvider tp)
        : base(cf, nav, snack, cus, tp)
    {
        _client = new RestClient(nav.BaseUri);
    }

    public async Task<ApiResponse<List<GetCompanyListResponse>>?> GetCompanyListAsync()
    {
        var request = new RestRequest("api/company", Method.Get);
        return await ExecuteWithResponseAsync<List<GetCompanyListResponse>>(_client, request);
    }

    public async Task<ApiResponse<GetCompanyByIdResponse>?> GetCompanyByIdAsync(string id)
    {
        var request = new RestRequest($"api/company/{id}", Method.Get);
        return await ExecuteWithResponseAsync<GetCompanyByIdResponse>(_client, request);
    }

    public async Task<ApiResponse<CreateCompanyResponse>?> CreateCompanyAsync(CreateCompanyRequest data)
    {
        var request = new RestRequest("api/company", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateCompanyResponse>(_client, request);
    }

    public async Task<ApiResponse<UpdateCompanyResponse>?> UpdateCompanyAsync(UpdateCompanyRequest data)
    {
        var request = new RestRequest("api/company/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<UpdateCompanyResponse>(_client, request);
    }

    public async Task<bool> DeleteCompanyByIdAsync(string id)
    {
        var request = new RestRequest($"api/company/delete/{id}", Method.Post);
        var response = await ExecuteWithResponseAsync<object>(_client, request);
        return response?.IsSuccess ?? false;
    }
}