import { invoke } from '@tauri-apps/api/core'

export interface Project {
  id: string
  name: string
  local_directory: string
  product_name: string
  product_image: string
  created_at: string
  updated_at: string
  is_active: boolean
}

export interface CreateProjectRequest {
  name: string
  local_directory: string
  product_name?: string
  product_image?: string
}

export interface UpdateProjectRequest {
  name?: string
  local_directory?: string
  product_name?: string
  product_image?: string
}

export interface ApiResponse<T> {
  status: boolean
  data?: T
  msg?: string
}

export class ProjectService {
  /**
   * 获取所有项目
   */
  static async getAllProjects(): Promise<ApiResponse<Project[]>> {
    try {
      const result = await invoke('get_all_projects', {  })
      return { status: true, msg: 'ok', data: this.tryJsonParse(result) } as ApiResponse<Project[]>
    } catch (error) {
      return {
        status: false,
        msg: error instanceof Error ? error.message : 'Unknown error'
      }
    }
  }

  /**
   * 根据ID获取项目
   */
  static async getProjectById(projectId: string): Promise<ApiResponse<Project>> {
    try {
      const result = await invoke('get_project_by_id', { projectId })
      return { status: true, msg: 'ok', data: this.tryJsonParse(result) } as ApiResponse<Project>
    } catch (error) {
      return {
        status: false,
        msg: error instanceof Error ? error.message : 'Unknown error'
      }
    }
  }



  /**
   * 创建新项目
   */
  static async createProject(request: CreateProjectRequest): Promise<ApiResponse<Project>> {
    try {
      const result = await invoke('create_project', { request })
      return { status: true, msg: 'ok', data: this.tryJsonParse(result) } as ApiResponse<Project>
    } catch (error) {
      return {
        status: false,
        msg: error instanceof Error ? error.message : 'Unknown error'
      }
    }
  }

  /**
   * 更新项目
   */
  static async updateProject(
    projectId: string,
    request: UpdateProjectRequest
  ): Promise<ApiResponse<Project>> {
    try {
      const result = await invoke('update_project', { projectId, request })
      return { status: true, msg: 'ok', data: this.tryJsonParse(result) } as ApiResponse<Project>
    } catch (error) {
      return {
        status: false,
        msg: error instanceof Error ? error.message : 'Unknown error'
      }
    }
  }

  /**
   * 删除项目
   */
  static async deleteProject(projectId: string): Promise<ApiResponse<boolean>> {
    try {
      const result = await invoke('search_projects', { projectId })
      return { status: true, msg: 'ok', data: this.tryJsonParse(result) } as ApiResponse<boolean>
    } catch (error) {
      return {
        status: false,
        msg: error instanceof Error ? error.message : 'Unknown error'
      }
    }
  }

  /**
   * 搜索项目
   */
  static async searchProjects(keyword: string): Promise<ApiResponse<Project[]>> {
    try {
      const result = await invoke('search_projects', { keyword })
      return { status: true, msg: 'ok', data: this.tryJsonParse(result) } as ApiResponse<Project[]>
    } catch (error) {
      return {
        status: false,
        msg: error instanceof Error ? error.message : 'Unknown error'
      }
    }
  }

  /**
   * 打开项目目录
   */
  static async openProjectDirectory(projectId: string): Promise<ApiResponse<boolean>> {
    try {
      const result = await invoke('open_project_directory', { projectId })
      return { status: true, msg: 'ok', data: this.tryJsonParse(result) } as ApiResponse<boolean>
    } catch (error) {
      return {
        status: false,
        msg: error instanceof Error ? error.message : 'Unknown error'
      }
    }
  }

  static tryJsonParse(str: any) {
    try {
      if (typeof str === 'string') {
        return JSON.parse(str)
      }
      return str;
    } catch (e) {
      return str;
    }
  }
}
