import { request } from '../base'
import type { ResultDomain, PageRequest } from '../../types'
import type { TbWorkcaseDTO, TbWorkcaseProcessDTO, TbWorkcaseDeviceDTO } from '../../types/workcase'

/**
 * @description    工单管理接口
 * @filename    workcase.ts
 * @author    yslg
 * @copyright xyzh
 * @since 2025-12-19
 */
export const workcaseAPI = {
    baseUrl: '/urban-lifeline/workcase',

    // ========================= 工单管理 =========================

    /**
     * 创建工单
     * @param workcase 工单信息
     */
    createWorkcase(workcase: TbWorkcaseDTO): Promise<ResultDomain<TbWorkcaseDTO>> {
        return request<TbWorkcaseDTO>({ url: this.baseUrl, method: 'POST', data: workcase })
    },

    /**
     * 更新工单
     * @param workcase 工单信息
     */
    updateWorkcase(workcase: TbWorkcaseDTO): Promise<ResultDomain<TbWorkcaseDTO>> {
        return request<TbWorkcaseDTO>({ url: this.baseUrl, method: 'PUT', data: workcase })
    },

    /**
     * 删除工单
     * @param workcaseId 工单ID
     */
    deleteWorkcase(workcaseId: string): Promise<ResultDomain<TbWorkcaseDTO>> {
        return request<TbWorkcaseDTO>({ url: `${this.baseUrl}/${workcaseId}`, method: 'DELETE' })
    },

    /**
     * 获取工单详情
     * @param workcaseId 工单ID
     */
    getWorkcaseById(workcaseId: string): Promise<ResultDomain<TbWorkcaseDTO>> {
        return request<TbWorkcaseDTO>({ url: `${this.baseUrl}/${workcaseId}`, method: 'GET' })
    },

    /**
     * 查询工单列表
     * @param filter 筛选条件
     */
    getWorkcaseList(filter?: TbWorkcaseDTO): Promise<ResultDomain<TbWorkcaseDTO>> {
        return request<TbWorkcaseDTO>({ url: `${this.baseUrl}/list`, method: 'POST', data: filter || {} })
    },

    /**
     * 分页查询工单
     * @param pageRequest 分页请求
     */
    getWorkcasePage(pageRequest: PageRequest<TbWorkcaseDTO>): Promise<ResultDomain<TbWorkcaseDTO>> {
        return request<TbWorkcaseDTO>({ url: `${this.baseUrl}/page`, method: 'POST', data: pageRequest })
    },

    // ========================= CRM同步接口 =========================

    /**
     * 同步工单到CRM
     * @param workcase 工单信息
     */
    syncWorkcaseToCrm(workcase: TbWorkcaseDTO): Promise<ResultDomain<void>> {
        return request<void>({ url: `${this.baseUrl}/sync/crm`, method: 'POST', data: workcase })
    },

    /**
     * 接收CRM工单更新（CRM回调）
     * @param jsonBody JSON字符串
     */
    receiveWorkcaseFromCrm(jsonBody: string): Promise<ResultDomain<void>> {
        return request<void>({ url: `${this.baseUrl}/receive/crm`, method: 'POST', data: jsonBody })
    },

    // ========================= 工单处理过程 =========================

    /**
     * 创建工单处理过程
     * @param process 处理过程信息
     */
    createWorkcaseProcess(process: TbWorkcaseProcessDTO): Promise<ResultDomain<TbWorkcaseProcessDTO>> {
        return request<TbWorkcaseProcessDTO>({ url: `${this.baseUrl}/process`, method: 'POST', data: process })
    },

    /**
     * 更新工单处理过程
     * @param process 处理过程信息
     */
    updateWorkcaseProcess(process: TbWorkcaseProcessDTO): Promise<ResultDomain<TbWorkcaseProcessDTO>> {
        return request<TbWorkcaseProcessDTO>({ url: `${this.baseUrl}/process`, method: 'PUT', data: process })
    },

    /**
     * 删除工单处理过程
     * @param processId 处理过程ID
     */
    deleteWorkcaseProcess(processId: string): Promise<ResultDomain<TbWorkcaseProcessDTO>> {
        return request<TbWorkcaseProcessDTO>({ url: `${this.baseUrl}/process/${processId}`, method: 'DELETE' })
    },

    /**
     * 查询工单处理过程列表
     * @param filter 筛选条件
     */
    getWorkcaseProcessList(filter?: TbWorkcaseProcessDTO): Promise<ResultDomain<TbWorkcaseProcessDTO>> {
        return request<TbWorkcaseProcessDTO>({ url: `${this.baseUrl}/process/list`, method: 'POST', data: filter || {} })
    },

    /**
     * 分页查询工单处理过程
     * @param pageRequest 分页请求
     */
    getWorkcaseProcessPage(pageRequest: PageRequest<TbWorkcaseProcessDTO>): Promise<ResultDomain<TbWorkcaseProcessDTO>> {
        return request<TbWorkcaseProcessDTO>({ url: `${this.baseUrl}/process/page`, method: 'POST', data: pageRequest })
    },

    // ========================= 工单设备管理 =========================

    /**
     * 创建工单设备
     * @param device 设备信息
     */
    createWorkcaseDevice(device: TbWorkcaseDeviceDTO): Promise<ResultDomain<TbWorkcaseDeviceDTO>> {
        return request<TbWorkcaseDeviceDTO>({ url: `${this.baseUrl}/device`, method: 'POST', data: device })
    },

    /**
     * 更新工单设备
     * @param device 设备信息
     */
    updateWorkcaseDevice(device: TbWorkcaseDeviceDTO): Promise<ResultDomain<TbWorkcaseDeviceDTO>> {
        return request<TbWorkcaseDeviceDTO>({ url: `${this.baseUrl}/device`, method: 'PUT', data: device })
    },

    /**
     * 删除工单设备
     * @param workcaseId 工单ID
     * @param device 设备名称
     */
    deleteWorkcaseDevice(workcaseId: string, device: string): Promise<ResultDomain<TbWorkcaseDeviceDTO>> {
        return request<TbWorkcaseDeviceDTO>({ url: `${this.baseUrl}/device/${workcaseId}/${device}`, method: 'DELETE' })
    },

    /**
     * 查询工单设备列表
     * @param filter 筛选条件
     */
    getWorkcaseDeviceList(filter?: TbWorkcaseDeviceDTO): Promise<ResultDomain<TbWorkcaseDeviceDTO>> {
        return request<TbWorkcaseDeviceDTO>({ url: `${this.baseUrl}/device/list`, method: 'POST', data: filter || {} })
    },

    /**
     * 分页查询工单设备
     * @param pageRequest 分页请求
     */
    getWorkcaseDevicePage(pageRequest: PageRequest<TbWorkcaseDeviceDTO>): Promise<ResultDomain<TbWorkcaseDeviceDTO>> {
        return request<TbWorkcaseDeviceDTO>({ url: `${this.baseUrl}/device/page`, method: 'POST', data: pageRequest })
    }
}
