import { apiRequest } from "@/shared/api/request";
import { API_ENDPOINTS, API_OPERATIONS, apiEndpointPath } from "@/shared/api/generated/endpoints";
import type {
  AdminUserDto,
  ApiPage,
  CreateUserResultDto,
  EntityId,
  FinanceScopeAssignmentDto,
  PageQuery,
  ResetPasswordResultDto,
  UserFormPayload,
  UserFinanceScopeDto,
  UserAppScopeDto,
  UserStatus,
  UserUpdatePayload
} from "@/shared/api/types";

export function listUsers(query?: PageQuery): Promise<ApiPage<AdminUserDto>> {
  return apiRequest<ApiPage<AdminUserDto>>(apiEndpointPath(API_OPERATIONS.listUsers), { query });
}

export function getUser(id: EntityId): Promise<AdminUserDto> {
  return apiRequest<AdminUserDto>(apiEndpointPath(API_OPERATIONS.getUser, { id }));
}

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

export function updateUser(id: EntityId, payload: UserUpdatePayload): Promise<AdminUserDto> {
  const endpoint = API_ENDPOINTS.updateUser;
  return apiRequest<AdminUserDto, UserUpdatePayload>(apiEndpointPath(API_OPERATIONS.updateUser, { id }), {
    body: payload,
    method: endpoint.method
  });
}

export function updateUserStatus(id: EntityId, status: UserStatus): Promise<AdminUserDto> {
  const endpoint = API_ENDPOINTS.updateUserStatus;
  return apiRequest<AdminUserDto, { status: UserStatus }>(apiEndpointPath(API_OPERATIONS.updateUserStatus, { id }), {
    body: { status },
    method: endpoint.method
  });
}

export function resetUserPassword(id: EntityId, password?: string): Promise<ResetPasswordResultDto> {
  const endpoint = API_ENDPOINTS.resetUserPassword;
  return apiRequest<ResetPasswordResultDto, { password?: string }>(apiEndpointPath(API_OPERATIONS.resetUserPassword, { id }), {
    body: { password },
    method: endpoint.method
  });
}

export function batchUpdateUserStatus(ids: EntityId[], status: UserStatus): Promise<unknown> {
  const endpoint = API_ENDPOINTS.batchUpdateUserStatus;
  return apiRequest<unknown, { ids: EntityId[]; status: UserStatus }>(apiEndpointPath(API_OPERATIONS.batchUpdateUserStatus), {
    body: { ids, status },
    method: endpoint.method
  });
}

export function exportUsers(query?: PageQuery): Promise<Response> {
  return apiRequest(apiEndpointPath(API_OPERATIONS.exportUsers), { query, raw: true });
}

export interface FinanceScopeCatalogDto {
  all?: boolean;
  apps?: Array<{ appCode: string; appName?: string; logoUrl?: string }>;
  regions?: Array<{ appCode: string; countries?: string[]; name?: string; regionCode?: string; regionId: number }>;
  countries?: Array<{ appCode: string; countryCode: string; countryDisplayName?: string; countryName?: string; regionId?: number }>;
  scopes?: UserFinanceScopeDto[];
}

export function listUserFinanceScopes(id: EntityId): Promise<{ items: UserFinanceScopeDto[]; total: number }> {
  return apiRequest<{ items: UserFinanceScopeDto[]; total: number }>(apiEndpointPath(API_OPERATIONS.listUserFinanceScopes, { id }));
}

export function replaceUserFinanceScopes(id: EntityId, scopes: UserFinanceScopeDto[]): Promise<{ items: UserFinanceScopeDto[]; total: number }> {
  return apiRequest<{ items: UserFinanceScopeDto[]; total: number }, { scopes: UserFinanceScopeDto[] }>(apiEndpointPath(API_OPERATIONS.replaceUserFinanceScopes, { id }), {
    body: { scopes },
    method: "PUT"
  });
}

export function getFinanceScopeCatalog(): Promise<FinanceScopeCatalogDto> {
  return apiRequest<FinanceScopeCatalogDto>(apiEndpointPath(API_OPERATIONS.getFinanceScope));
}

export function listFinanceScopeAssignments(): Promise<{ items: FinanceScopeAssignmentDto[]; total: number }> {
  return apiRequest<{ items: FinanceScopeAssignmentDto[]; total: number }>(apiEndpointPath(API_OPERATIONS.listFinanceScopeAssignments));
}

export function getUserAppScopes(id: EntityId): Promise<UserAppScopeDto> {
  return apiRequest<UserAppScopeDto>(apiEndpointPath(API_OPERATIONS.getUserAppScopes, { id }));
}

export function replaceUserAppScopes(id: EntityId, scope: UserAppScopeDto): Promise<UserAppScopeDto> {
  return apiRequest<UserAppScopeDto, UserAppScopeDto>(apiEndpointPath(API_OPERATIONS.replaceUserAppScopes, { id }), {
    body: scope,
    method: "PUT"
  });
}
