/**
 * 店铺相关 API - 匹配后端实际接口
 */

import { BaseService } from '../BaseService'
import type {
  ShopResponse,
  ShopQueryParams,
  CreateShopParams,
  UpdateShopParams,
  ShopRolesResponse,
  AssignShopRolesRequest,
  UpdateShopCreditLimitRequest,
  BaseResponse,
  PaginationResponse
} from '@/types/api'

export class ShopService extends BaseService {
  /**
   * 获取店铺列表
   * GET /api/admin/shops
   * @param params 查询参数
   */
  static getShops(params?: ShopQueryParams): Promise<PaginationResponse<ShopResponse>> {
    return this.getPage<ShopResponse>('/api/admin/shops', params)
  }

  /**
   * 店铺级联查询（树结构）
   * GET /api/admin/shops/cascade
   * @param params 查询参数
   */
  static getShopsCascade(params?: {
    shop_name?: string
    parent_id?: number
    exclude_self?: boolean
  }): Promise<
    BaseResponse<
      Array<{
        id: number
        shop_name: string
        has_children: boolean
      }>
    >
  > {
    return this.get<
      BaseResponse<
        Array<{
          id: number
          shop_name: string
          has_children: boolean
        }>
      >
    >('/api/admin/shops/cascade', params)
  }

  /**
   * 创建店铺
   * POST /api/admin/shops
   * @param data 店铺数据
   */
  static createShop(data: CreateShopParams): Promise<BaseResponse<ShopResponse>> {
    return this.post<BaseResponse<ShopResponse>>('/api/admin/shops', data)
  }

  /**
   * 更新店铺
   * PUT /api/admin/shops/{id}
   * @param id 店铺ID
   * @param data 更新数据
   */
  static updateShop(id: number, data: UpdateShopParams): Promise<BaseResponse<ShopResponse>> {
    return this.put<BaseResponse<ShopResponse>>(`/api/admin/shops/${id}`, data)
  }

  /**
   * 删除店铺
   * DELETE /api/admin/shops/{id}
   * @param id 店铺ID
   */
  static deleteShop(id: number): Promise<BaseResponse> {
    return this.delete<BaseResponse>(`/api/admin/shops/${id}`)
  }

  // ========== 店铺默认角色管理 ==========

  /**
   * 获取店铺默认角色列表
   * GET /api/admin/shops/{shop_id}/roles
   * @param shopId 店铺ID
   */
  static getShopRoles(shopId: number): Promise<BaseResponse<ShopRolesResponse>> {
    return this.get<BaseResponse<ShopRolesResponse>>(`/api/admin/shops/${shopId}/roles`)
  }

  /**
   * 分配店铺默认角色
   * POST /api/admin/shops/{shop_id}/roles
   * @param shopId 店铺ID
   * @param data 角色ID列表
   */
  static assignShopRoles(
    shopId: number,
    data: AssignShopRolesRequest
  ): Promise<BaseResponse<ShopRolesResponse>> {
    return this.post<BaseResponse<ShopRolesResponse>>(`/api/admin/shops/${shopId}/roles`, data)
  }

  /**
   * 删除店铺默认角色
   * DELETE /api/admin/shops/{shop_id}/roles/{role_id}
   * @param shopId 店铺ID
   * @param roleId 角色ID
   */
  static deleteShopRole(shopId: number, roleId: number): Promise<BaseResponse> {
    return this.delete<BaseResponse>(`/api/admin/shops/${shopId}/roles/${roleId}`)
  }

  /**
   * 更新店铺实际信用额度
   * PUT /api/admin/shops/{id}/credit-limit
   * @param shopId 店铺ID
   * @param data 实际信用额度配置
   */
  static updateShopCreditLimit(
    shopId: number,
    data: UpdateShopCreditLimitRequest
  ): Promise<BaseResponse> {
    return this.put<BaseResponse>(`/api/admin/shops/${shopId}/credit-limit`, data)
  }
}
