/**
 * 账号相关 API - 匹配后端实际接口
 */

import { BaseService } from '../BaseService'
import type {
  PlatformAccount,
  AccountQueryParams,
  PlatformSalesperson,
  PlatformSalespersonQueryParams,
  CreatePlatformAccountParams,
  BaseResponse,
  PaginationResponse
} from '@/types/api'

export class AccountService extends BaseService {
  // ========== 账号管理 (Account Management) ==========

  /**
   * 获取账号列表
   * GET /api/admin/accounts
   * @param params 查询参数
   */
  static getAccounts(params?: AccountQueryParams): Promise<PaginationResponse<PlatformAccount>> {
    return this.getPage<PlatformAccount>('/api/admin/accounts', params)
  }

  /**
   * 获取启用的平台业务员候选
   * GET /api/admin/accounts?account_type=platform&status=1
   */
  static getPlatformSalespeople(
    params: PlatformSalespersonQueryParams
  ): Promise<PaginationResponse<PlatformSalesperson>> {
    return this.getPage<PlatformSalesperson>('/api/admin/accounts', params)
  }

  /**
   * 创建账号
   * POST /api/admin/accounts
   * @param data 账号数据
   */
  static createAccount(
    data: CreatePlatformAccountParams
  ): Promise<BaseResponse<{ ID?: number; id?: number }>> {
    return this.create('/api/admin/accounts', data)
  }

  /**
   * 更新账号
   * PUT /api/admin/accounts/{id}
   * @param id 账号ID
   * @param data 账号数据
   */
  static updateAccount(
    id: number,
    data: Partial<CreatePlatformAccountParams>
  ): Promise<BaseResponse> {
    return this.update(`/api/admin/accounts/${id}`, data)
  }

  /**
   * 删除账号
   * DELETE /api/admin/accounts/{id}
   * @param id 账号ID
   */
  static deleteAccount(id: number): Promise<BaseResponse> {
    return this.remove(`/api/admin/accounts/${id}`)
  }

  /**
   * 获取账号详情
   * GET /api/admin/accounts/{id}
   * @param id 账号ID
   */
  static getAccountDetail(id: number): Promise<BaseResponse<PlatformAccount>> {
    return this.getOne<PlatformAccount>(`/api/admin/accounts/${id}`)
  }

  /**
   * 获取账号的角色列表
   * GET /api/admin/accounts/{id}/roles
   * @param id 账号ID
   * @returns 返回角色对象数组
   */
  static getAccountRoles(id: number): Promise<BaseResponse<any[]>> {
    return this.get<BaseResponse<any[]>>(`/api/admin/accounts/${id}/roles`)
  }

  /**
   * 为账号分配角色
   * POST /api/admin/accounts/{id}/roles
   * @param id 账号ID
   * @param roleIds 角色ID列表
   */
  static assignRolesToAccount(id: number, roleIds: number[]): Promise<BaseResponse> {
    return this.post<BaseResponse>(`/api/admin/accounts/${id}/roles`, { role_ids: roleIds })
  }

  /**
   * 移除账号的单个角色
   * DELETE /api/admin/accounts/{account_id}/roles/{role_id}
   * @param accountId 账号ID
   * @param roleId 角色ID
   */
  static removeRoleFromAccount(accountId: number, roleId: number): Promise<BaseResponse> {
    return this.delete<BaseResponse>(`/api/admin/accounts/${accountId}/roles/${roleId}`)
  }

  /**
   * 修改账号密码
   * PUT /api/admin/accounts/{id}/password
   * @param id 账号ID
   * @param password 新密码
   */
  static updateAccountPassword(id: number, password: string): Promise<BaseResponse> {
    return this.put<BaseResponse>(`/api/admin/accounts/${id}/password`, { password })
  }

  /**
   * 修改账号状态
   * PUT /api/admin/accounts/{id}/status
   * @param id 账号ID
   * @param status 状态 (0:禁用, 1:启用)
   */
  static updateAccountStatus(id: number, status: 0 | 1): Promise<BaseResponse> {
    return this.put<BaseResponse>(`/api/admin/accounts/${id}/status`, { status })
  }
}
