import { api } from 'shared/api'
import type { ResultDomain, PageRequest } from 'shared/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 工单信息
     */
    async createWorkcase(workcase: TbWorkcaseDTO): Promise<ResultDomain<TbWorkcaseDTO>> {
        const response = await api.post<TbWorkcaseDTO>(`${this.baseUrl}`, workcase)
        return response.data
    },

    /**
     * 更新工单
     * @param workcase 工单信息
     */
    async updateWorkcase(workcase: TbWorkcaseDTO): Promise<ResultDomain<TbWorkcaseDTO>> {
        const response = await api.put<TbWorkcaseDTO>(`${this.baseUrl}`, workcase)
        return response.data
    },

    /**
     * 删除工单
     * @param workcaseId 工单ID
     */
    async deleteWorkcase(workcaseId: string): Promise<ResultDomain<TbWorkcaseDTO>> {
        const response = await api.delete<TbWorkcaseDTO>(`${this.baseUrl}/${workcaseId}`)
        return response.data
    },

    /**
     * 获取工单详情
     * @param workcaseId 工单ID
     */
    async getWorkcaseById(workcaseId: string): Promise<ResultDomain<TbWorkcaseDTO>> {
        const response = await api.get<TbWorkcaseDTO>(`${this.baseUrl}/${workcaseId}`)
        return response.data
    },

    /**
     * 查询工单列表
     * @param filter 筛选条件
     */
    async getWorkcaseList(filter?: TbWorkcaseDTO): Promise<ResultDomain<TbWorkcaseDTO>> {
        const response = await api.post<TbWorkcaseDTO>(`${this.baseUrl}/list`, filter || {})
        return response.data
    },

    /**
     * 分页查询工单
     * @param pageRequest 分页请求
     */
    async getWorkcasePage(pageRequest: PageRequest<TbWorkcaseDTO>): Promise<ResultDomain<TbWorkcaseDTO>> {
        const response = await api.post<TbWorkcaseDTO>(`${this.baseUrl}/page`, pageRequest)
        return response.data
    },

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

    /**
     * 同步工单到CRM
     * @param workcase 工单信息
     */
    async syncWorkcaseToCrm(workcase: TbWorkcaseDTO): Promise<ResultDomain<void>> {
        const response = await api.post<void>(`${this.baseUrl}/sync/crm`, workcase)
        return response.data
    },

    /**
     * 接收CRM工单更新（CRM回调）
     * @param jsonBody JSON字符串
     */
    async receiveWorkcaseFromCrm(jsonBody: string): Promise<ResultDomain<void>> {
        const response = await api.post<void>(`${this.baseUrl}/receive/crm`, jsonBody)
        return response.data
    },

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

    /**
     * 创建工单处理过程
     * @param process 处理过程信息
     */
    async createWorkcaseProcess(process: TbWorkcaseProcessDTO): Promise<ResultDomain<TbWorkcaseProcessDTO>> {
        const response = await api.post<TbWorkcaseProcessDTO>(`${this.baseUrl}/process`, process)
        return response.data
    },

    /**
     * 更新工单处理过程
     * @param process 处理过程信息
     */
    async updateWorkcaseProcess(process: TbWorkcaseProcessDTO): Promise<ResultDomain<TbWorkcaseProcessDTO>> {
        const response = await api.put<TbWorkcaseProcessDTO>(`${this.baseUrl}/process`, process)
        return response.data
    },

    /**
     * 删除工单处理过程
     * @param processId 处理过程ID
     */
    async deleteWorkcaseProcess(processId: string): Promise<ResultDomain<TbWorkcaseProcessDTO>> {
        const response = await api.delete<TbWorkcaseProcessDTO>(`${this.baseUrl}/process/${processId}`)
        return response.data
    },

    /**
     * 查询工单处理过程列表
     * @param filter 筛选条件
     */
    async getWorkcaseProcessList(filter?: TbWorkcaseProcessDTO): Promise<ResultDomain<TbWorkcaseProcessDTO>> {
        const response = await api.post<TbWorkcaseProcessDTO>(`${this.baseUrl}/process/list`, filter || {})
        return response.data
    },

    /**
     * 分页查询工单处理过程
     * @param pageRequest 分页请求
     */
    async getWorkcaseProcessPage(pageRequest: PageRequest<TbWorkcaseProcessDTO>): Promise<ResultDomain<TbWorkcaseProcessDTO>> {
        const response = await api.post<TbWorkcaseProcessDTO>(`${this.baseUrl}/process/page`, pageRequest)
        return response.data
    },

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

    /**
     * 创建工单设备
     * @param device 设备信息
     */
    async createWorkcaseDevice(device: TbWorkcaseDeviceDTO): Promise<ResultDomain<TbWorkcaseDeviceDTO>> {
        const response = await api.post<TbWorkcaseDeviceDTO>(`${this.baseUrl}/device`, device)
        return response.data
    },

    /**
     * 更新工单设备
     * @param device 设备信息
     */
    async updateWorkcaseDevice(device: TbWorkcaseDeviceDTO): Promise<ResultDomain<TbWorkcaseDeviceDTO>> {
        const response = await api.put<TbWorkcaseDeviceDTO>(`${this.baseUrl}/device`, device)
        return response.data
    },

    /**
     * 删除工单设备
     * @param workcaseId 工单ID
     * @param device 设备名称
     */
    async deleteWorkcaseDevice(workcaseId: string, device: string): Promise<ResultDomain<TbWorkcaseDeviceDTO>> {
        const response = await api.delete<TbWorkcaseDeviceDTO>(`${this.baseUrl}/device/${workcaseId}/${device}`)
        return response.data
    },

    /**
     * 查询工单设备列表
     * @param filter 筛选条件
     */
    async getWorkcaseDeviceList(filter?: TbWorkcaseDeviceDTO): Promise<ResultDomain<TbWorkcaseDeviceDTO>> {
        const response = await api.post<TbWorkcaseDeviceDTO>(`${this.baseUrl}/device/list`, filter || {})
        return response.data
    },

    /**
     * 分页查询工单设备
     * @param pageRequest 分页请求
     */
    async getWorkcaseDevicePage(pageRequest: PageRequest<TbWorkcaseDeviceDTO>): Promise<ResultDomain<TbWorkcaseDeviceDTO>> {
        const response = await api.post<TbWorkcaseDeviceDTO>(`${this.baseUrl}/device/page`, pageRequest)
        return response.data
    }
}
