import { apiRequest } from "@/shared/api/request";
import { API_ENDPOINTS, API_OPERATIONS, apiEndpointPath } from "@/shared/api/generated/endpoints";
export { listRegions } from "@/shared/api/regions";
import type {
    AgencyDto,
    AgencyHostAddPayload,
    AgencyJoinEnabledPayload,
    AgencyStatusPayload,
    AdminJobDto,
    ApiList,
    ApiPage,
    BDLeaderPositionAliasPayload,
    BDProfileDto,
    BDStatusPayload,
    CoinSellerDto,
    CoinSellerSalaryRatesDto,
    CoinSellerSalaryRatesPayload,
    CoinSellerSubApplicationDto,
    CoinSellerSubApplicationReviewPayload,
    CoinSellerSubApplicationReviewResultDto,
    CoinSellerStockCreditDto,
    CoinSellerStockCreditPayload,
    CoinSellerStockDebitDto,
    CoinSellerStockDebitPayload,
    CoinSellerStatusPayload,
    CountryCodeRenameJobDto,
    CountryDto,
    CountryPayload,
    CountryRenameCodePayload,
    CountryUpdatePayload,
    CreateAgencyPayload,
    CreateAgencyResultDto,
    CreateBDLeaderPayload,
    CreateBDPayload,
    CreateCoinSellerPayload,
    CreateManagerPayload,
    EntityId,
    HostCommandPayload,
    HostWithdrawalDto,
    HostProfileDto,
    ManagerDto,
    PageQuery,
    RegionCountriesPayload,
    RegionDto,
    RegionPayload,
    RegionUpdatePayload,
    SalaryWalletAdjustPayload,
    SalaryWalletAdjustResultDto,
    SalaryWalletHistoryDto,
    UpdateManagerPayload,
} from "@/shared/api/types";

export function listCountries(query: PageQuery = {}): Promise<ApiList<CountryDto>> {
    const endpoint = API_ENDPOINTS.listCountries;
    return apiRequest<ApiList<CountryDto>>(apiEndpointPath(API_OPERATIONS.listCountries), {
        method: endpoint.method,
        query,
    });
}

export function getCountry(countryId: EntityId): Promise<CountryDto> {
    const endpoint = API_ENDPOINTS.getCountry;
    return apiRequest<CountryDto>(apiEndpointPath(API_OPERATIONS.getCountry, { country_id: countryId }), {
        method: endpoint.method,
    });
}

export function createCountry(payload: CountryPayload): Promise<CountryDto> {
    const endpoint = API_ENDPOINTS.createCountry;
    return apiRequest<CountryDto, CountryPayload>(apiEndpointPath(API_OPERATIONS.createCountry), {
        body: payload,
        method: endpoint.method,
    });
}

export function updateCountry(countryId: EntityId, payload: CountryUpdatePayload): Promise<CountryDto> {
    const endpoint = API_ENDPOINTS.updateCountry;
    return apiRequest<CountryDto, CountryUpdatePayload>(
        apiEndpointPath(API_OPERATIONS.updateCountry, { country_id: countryId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function renameCountryCode(
    countryId: EntityId,
    payload: CountryRenameCodePayload,
): Promise<CountryCodeRenameJobDto> {
    const endpoint = API_ENDPOINTS.renameCountryCode;
    return apiRequest<CountryCodeRenameJobDto, CountryRenameCodePayload>(
        apiEndpointPath(API_OPERATIONS.renameCountryCode, { country_id: countryId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function getJob(jobId: EntityId): Promise<AdminJobDto> {
    const endpoint = API_ENDPOINTS.getJob;
    return apiRequest<AdminJobDto>(apiEndpointPath(API_OPERATIONS.getJob, { id: jobId }), {
        method: endpoint.method,
    });
}

export function enableCountry(countryId: EntityId): Promise<CountryDto> {
    const endpoint = API_ENDPOINTS.enableCountry;
    return apiRequest<CountryDto>(apiEndpointPath(API_OPERATIONS.enableCountry, { country_id: countryId }), {
        method: endpoint.method,
    });
}

export function disableCountry(countryId: EntityId): Promise<CountryDto> {
    const endpoint = API_ENDPOINTS.disableCountry;
    return apiRequest<CountryDto>(apiEndpointPath(API_OPERATIONS.disableCountry, { country_id: countryId }), {
        method: endpoint.method,
    });
}

export function deleteCountry(countryId: EntityId): Promise<CountryDto> {
    const endpoint = API_ENDPOINTS.deleteCountry;
    return apiRequest<CountryDto>(apiEndpointPath(API_OPERATIONS.deleteCountry, { country_id: countryId }), {
        method: endpoint.method,
    });
}

export function getRegion(regionId: EntityId): Promise<RegionDto> {
    const endpoint = API_ENDPOINTS.getRegion;
    return apiRequest<RegionDto>(apiEndpointPath(API_OPERATIONS.getRegion, { region_id: regionId }), {
        method: endpoint.method,
    });
}

export function createRegion(payload: RegionPayload): Promise<RegionDto> {
    const endpoint = API_ENDPOINTS.createRegion;
    return apiRequest<RegionDto, RegionPayload>(apiEndpointPath(API_OPERATIONS.createRegion), {
        body: payload,
        method: endpoint.method,
    });
}

export function updateRegion(regionId: EntityId, payload: RegionUpdatePayload): Promise<RegionDto> {
    const endpoint = API_ENDPOINTS.updateRegion;
    return apiRequest<RegionDto, RegionUpdatePayload>(
        apiEndpointPath(API_OPERATIONS.updateRegion, { region_id: regionId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function replaceRegionCountries(regionId: EntityId, payload: RegionCountriesPayload): Promise<RegionDto> {
    const endpoint = API_ENDPOINTS.replaceRegionCountries;
    return apiRequest<RegionDto, RegionCountriesPayload>(
        apiEndpointPath(API_OPERATIONS.replaceRegionCountries, { region_id: regionId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function enableRegion(regionId: EntityId): Promise<RegionDto> {
    const endpoint = API_ENDPOINTS.enableRegion;
    return apiRequest<RegionDto>(apiEndpointPath(API_OPERATIONS.enableRegion, { region_id: regionId }), {
        method: endpoint.method,
    });
}

export function disableRegion(regionId: EntityId): Promise<RegionDto> {
    const endpoint = API_ENDPOINTS.disableRegion;
    return apiRequest<RegionDto>(apiEndpointPath(API_OPERATIONS.disableRegion, { region_id: regionId }), {
        method: endpoint.method,
    });
}

export function listBDLeaders(query: PageQuery = {}): Promise<ApiPage<BDProfileDto>> {
    const endpoint = API_ENDPOINTS.listBDLeaders;
    return apiRequest<ApiPage<BDProfileDto>>(apiEndpointPath(API_OPERATIONS.listBDLeaders), {
        method: endpoint.method,
        query,
    });
}

export function listBDs(query: PageQuery = {}): Promise<ApiPage<BDProfileDto>> {
    const endpoint = API_ENDPOINTS.listBDs;
    return apiRequest<ApiPage<BDProfileDto>>(apiEndpointPath(API_OPERATIONS.listBDs), {
        method: endpoint.method,
        query,
    });
}

export function listAgencies(query: PageQuery = {}): Promise<ApiPage<AgencyDto>> {
    const endpoint = API_ENDPOINTS.listAgencies;
    return apiRequest<ApiPage<AgencyDto>>(apiEndpointPath(API_OPERATIONS.listAgencies), {
        method: endpoint.method,
        query,
    });
}

export function listManagers(query: PageQuery = {}): Promise<ApiPage<ManagerDto>> {
    const endpoint = API_ENDPOINTS.listManagers;
    return apiRequest<ApiPage<ManagerDto>>(apiEndpointPath(API_OPERATIONS.listManagers), {
        method: endpoint.method,
        query,
    });
}

export function createManager(payload: CreateManagerPayload): Promise<ManagerDto> {
    const endpoint = API_ENDPOINTS.createManager;
    return apiRequest<ManagerDto, CreateManagerPayload>(apiEndpointPath(API_OPERATIONS.createManager), {
        body: payload,
        method: endpoint.method,
    });
}

export function updateManager(userId: EntityId, payload: UpdateManagerPayload): Promise<ManagerDto> {
    const endpoint = API_ENDPOINTS.updateManager;
    return apiRequest<ManagerDto, UpdateManagerPayload>(
        apiEndpointPath(API_OPERATIONS.updateManager, { user_id: userId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function listHosts(query: PageQuery = {}): Promise<ApiPage<HostProfileDto>> {
    const endpoint = API_ENDPOINTS.listHosts;
    return apiRequest<ApiPage<HostProfileDto>>(apiEndpointPath(API_OPERATIONS.listHosts), {
        method: endpoint.method,
        query,
    });
}

export function listCoinSellers(query: PageQuery = {}): Promise<ApiPage<CoinSellerDto>> {
    const endpoint = API_ENDPOINTS.listCoinSellers;
    return apiRequest<ApiPage<CoinSellerDto>>(apiEndpointPath(API_OPERATIONS.listCoinSellers), {
        method: endpoint.method,
        query,
    });
}

export function listCoinSellerSubSellers(userId: EntityId, query: PageQuery = {}): Promise<ApiPage<CoinSellerDto>> {
    const endpoint = API_ENDPOINTS.listCoinSellerSubSellers;
    return apiRequest<ApiPage<CoinSellerDto>>(
        apiEndpointPath(API_OPERATIONS.listCoinSellerSubSellers, { user_id: userId }),
        {
            method: endpoint.method,
            query,
        },
    );
}

export function listCoinSellerSubApplications(query: PageQuery = {}): Promise<ApiPage<CoinSellerSubApplicationDto>> {
    const endpoint = API_ENDPOINTS.listCoinSellerSubApplications;
    return apiRequest<ApiPage<CoinSellerSubApplicationDto>>(
        apiEndpointPath(API_OPERATIONS.listCoinSellerSubApplications),
        {
            method: endpoint.method,
            query,
        },
    );
}

export function listHostWithdrawals(query: PageQuery = {}): Promise<ApiPage<HostWithdrawalDto>> {
    const endpoint = API_ENDPOINTS.listHostWithdrawals;
    return apiRequest<ApiPage<HostWithdrawalDto>>(apiEndpointPath(API_OPERATIONS.listHostWithdrawals), {
        method: endpoint.method,
        query,
    });
}

export function listSalaryWalletHistory(query: PageQuery = {}): Promise<ApiPage<SalaryWalletHistoryDto>> {
    return apiRequest<ApiPage<SalaryWalletHistoryDto>>("/v1/admin/host-org/salary-wallets/history", {
        method: "GET",
        query,
    });
}

export function adjustSalaryWallet(payload: SalaryWalletAdjustPayload): Promise<SalaryWalletAdjustResultDto> {
    return apiRequest<SalaryWalletAdjustResultDto, SalaryWalletAdjustPayload>(
        "/v1/admin/host-org/salary-wallets/adjust",
        {
            body: payload,
            method: "POST",
        },
    );
}

export function createBDLeader(payload: CreateBDLeaderPayload): Promise<BDProfileDto> {
    const endpoint = API_ENDPOINTS.createBDLeader;
    return apiRequest<BDProfileDto, CreateBDLeaderPayload>(apiEndpointPath(API_OPERATIONS.createBDLeader), {
        body: payload,
        method: endpoint.method,
    });
}

export function createBD(payload: CreateBDPayload): Promise<BDProfileDto> {
    const endpoint = API_ENDPOINTS.createBD;
    return apiRequest<BDProfileDto, CreateBDPayload>(apiEndpointPath(API_OPERATIONS.createBD), {
        body: payload,
        method: endpoint.method,
    });
}

export function deleteBDLeader(userId: EntityId): Promise<BDProfileDto> {
    return apiRequest<BDProfileDto>(`/v1/admin/bd-leaders/${encodeURIComponent(String(userId))}`, {
        method: "DELETE",
    });
}

export function updateBDLeaderPositionAlias(
    userId: EntityId,
    payload: BDLeaderPositionAliasPayload,
): Promise<BDProfileDto> {
    return apiRequest<BDProfileDto, BDLeaderPositionAliasPayload>(
        `/v1/admin/bd-leaders/${encodeURIComponent(String(userId))}/position-alias`,
        {
            body: payload,
            method: "PATCH",
        },
    );
}

export function setBDLeaderStatus(userId: EntityId, payload: BDStatusPayload): Promise<BDProfileDto> {
    const endpoint = API_ENDPOINTS.setBDLeaderStatus;
    return apiRequest<BDProfileDto, BDStatusPayload>(
        apiEndpointPath(API_OPERATIONS.setBDLeaderStatus, { user_id: userId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function setBDStatus(userId: EntityId, payload: BDStatusPayload): Promise<BDProfileDto> {
    const endpoint = API_ENDPOINTS.setBDStatus;
    return apiRequest<BDProfileDto, BDStatusPayload>(apiEndpointPath(API_OPERATIONS.setBDStatus, { user_id: userId }), {
        body: payload,
        method: endpoint.method,
    });
}

export function createCoinSeller(payload: CreateCoinSellerPayload): Promise<CoinSellerDto> {
    const endpoint = API_ENDPOINTS.createCoinSeller;
    return apiRequest<CoinSellerDto, CreateCoinSellerPayload>(apiEndpointPath(API_OPERATIONS.createCoinSeller), {
        body: payload,
        method: endpoint.method,
    });
}

export function setCoinSellerStatus(userId: EntityId, payload: CoinSellerStatusPayload): Promise<CoinSellerDto> {
    const endpoint = API_ENDPOINTS.setCoinSellerStatus;
    return apiRequest<CoinSellerDto, CoinSellerStatusPayload>(
        apiEndpointPath(API_OPERATIONS.setCoinSellerStatus, { user_id: userId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function approveCoinSellerSubApplication(
    applicationId: EntityId,
    payload: CoinSellerSubApplicationReviewPayload,
): Promise<CoinSellerSubApplicationReviewResultDto> {
    const endpoint = API_ENDPOINTS.approveCoinSellerSubApplication;
    return apiRequest<CoinSellerSubApplicationReviewResultDto, CoinSellerSubApplicationReviewPayload>(
        apiEndpointPath(API_OPERATIONS.approveCoinSellerSubApplication, { application_id: applicationId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function rejectCoinSellerSubApplication(
    applicationId: EntityId,
    payload: CoinSellerSubApplicationReviewPayload,
): Promise<CoinSellerSubApplicationReviewResultDto> {
    const endpoint = API_ENDPOINTS.rejectCoinSellerSubApplication;
    return apiRequest<CoinSellerSubApplicationReviewResultDto, CoinSellerSubApplicationReviewPayload>(
        apiEndpointPath(API_OPERATIONS.rejectCoinSellerSubApplication, { application_id: applicationId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function creditCoinSellerStock(
    userId: EntityId,
    payload: CoinSellerStockCreditPayload,
): Promise<CoinSellerStockCreditDto> {
    const endpoint = API_ENDPOINTS.creditCoinSellerStock;
    return apiRequest<CoinSellerStockCreditDto, CoinSellerStockCreditPayload>(
        apiEndpointPath(API_OPERATIONS.creditCoinSellerStock, { user_id: userId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function debitCoinSellerStock(
    userId: EntityId,
    payload: CoinSellerStockDebitPayload,
): Promise<CoinSellerStockDebitDto> {
    const endpoint = API_ENDPOINTS.debitCoinSellerStock;
    return apiRequest<CoinSellerStockDebitDto, CoinSellerStockDebitPayload>(
        apiEndpointPath(API_OPERATIONS.debitCoinSellerStock, { user_id: userId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function getCoinSellerSalaryRates(regionId: EntityId): Promise<CoinSellerSalaryRatesDto> {
    const endpoint = API_ENDPOINTS.getCoinSellerSalaryRates;
    return apiRequest<CoinSellerSalaryRatesDto>(
        apiEndpointPath(API_OPERATIONS.getCoinSellerSalaryRates, { region_id: regionId }),
        {
            method: endpoint.method,
        },
    );
}

export function replaceCoinSellerSalaryRates(
    regionId: EntityId,
    payload: CoinSellerSalaryRatesPayload,
): Promise<CoinSellerSalaryRatesDto> {
    const endpoint = API_ENDPOINTS.replaceCoinSellerSalaryRates;
    return apiRequest<CoinSellerSalaryRatesDto, CoinSellerSalaryRatesPayload>(
        apiEndpointPath(API_OPERATIONS.replaceCoinSellerSalaryRates, { region_id: regionId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function createAgency(payload: CreateAgencyPayload): Promise<CreateAgencyResultDto> {
    const endpoint = API_ENDPOINTS.createAgency;
    return apiRequest<CreateAgencyResultDto, CreateAgencyPayload>(apiEndpointPath(API_OPERATIONS.createAgency), {
        body: payload,
        method: endpoint.method,
    });
}

export function adminAddAgencyHost(agencyId: EntityId, payload: AgencyHostAddPayload): Promise<CreateAgencyResultDto> {
    const endpoint = API_ENDPOINTS.adminAddAgencyHost;
    return apiRequest<CreateAgencyResultDto, AgencyHostAddPayload>(
        apiEndpointPath(API_OPERATIONS.adminAddAgencyHost, { agency_id: agencyId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function closeAgency(agencyId: EntityId, payload: HostCommandPayload): Promise<AgencyDto> {
    const endpoint = API_ENDPOINTS.closeAgency;
    return apiRequest<AgencyDto, HostCommandPayload>(
        apiEndpointPath(API_OPERATIONS.closeAgency, { agency_id: agencyId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function deleteAgency(agencyId: EntityId, payload: HostCommandPayload): Promise<AgencyDto> {
    const endpoint = API_ENDPOINTS.deleteAgency;
    return apiRequest<AgencyDto, HostCommandPayload>(
        apiEndpointPath(API_OPERATIONS.deleteAgency, { agency_id: agencyId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function setAgencyStatus(agencyId: EntityId, payload: AgencyStatusPayload): Promise<AgencyDto> {
    const endpoint = API_ENDPOINTS.setAgencyStatus;
    return apiRequest<AgencyDto, AgencyStatusPayload>(
        apiEndpointPath(API_OPERATIONS.setAgencyStatus, { agency_id: agencyId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function setAgencyJoinEnabled(agencyId: EntityId, payload: AgencyJoinEnabledPayload): Promise<AgencyDto> {
    const endpoint = API_ENDPOINTS.setAgencyJoinEnabled;
    return apiRequest<AgencyDto, AgencyJoinEnabledPayload>(
        apiEndpointPath(API_OPERATIONS.setAgencyJoinEnabled, { agency_id: agencyId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}
