/**
 * Copyright (c) 2023-present Plane Software, Inc. and contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 * See the LICENSE file for details.
 */

import { API_BASE_URL } from "@plane/constants";
import type {
  TExternalContourRequest,
  TExternalContourRequestResponse,
  TExternalContourTargetOptions,
  TExternalContourTargetProject,
  TIssue,
} from "@plane/types";
import { APIService } from "@/services/api.service";

export class ExternalContourService extends APIService {
  constructor() {
    super(API_BASE_URL);
  }

  async list(workspaceSlug: string, projectId: string): Promise<TExternalContourRequestResponse> {
    return this.get(`/api/workspaces/${workspaceSlug}/projects/${projectId}/external-contours/`)
      .then((response) => response?.data)
      .catch((error) => {
        throw error?.response?.data;
      });
  }

  async retrieve(workspaceSlug: string, projectId: string, requestId: string): Promise<TExternalContourRequest> {
    return this.get(`/api/workspaces/${workspaceSlug}/projects/${projectId}/external-contours/${requestId}/`)
      .then((response) => response?.data)
      .catch((error) => {
        throw error?.response?.data;
      });
  }

  async updateRequest(
    workspaceSlug: string,
    projectId: string,
    requestId: string,
    data: Pick<Partial<TIssue>, "name" | "description_html">
  ): Promise<TExternalContourRequest> {
    return this.patch(`/api/workspaces/${workspaceSlug}/projects/${projectId}/external-contours/${requestId}/`, data)
      .then((response) => response?.data)
      .catch((error) => {
        throw error?.response?.data;
      });
  }

  async listTargetProjects(workspaceSlug: string, projectId: string): Promise<TExternalContourTargetProject[]> {
    return this.get(`/api/workspaces/${workspaceSlug}/projects/${projectId}/external-contours/targets/`)
      .then((response) => response?.data)
      .catch((error) => {
        throw error?.response?.data;
      });
  }

  async retrieveTargetOptions(
    workspaceSlug: string,
    projectId: string,
    targetProjectId: string
  ): Promise<TExternalContourTargetOptions> {
    return this.get(`/api/workspaces/${workspaceSlug}/projects/${projectId}/external-contours/targets/${targetProjectId}/options/`)
      .then((response) => response?.data)
      .catch((error) => {
        throw error?.response?.data;
      });
  }

  async create(
    workspaceSlug: string,
    projectId: string,
    data: Partial<TIssue> & { target_project_id?: string | null }
  ): Promise<TExternalContourRequest> {
    return this.post(`/api/workspaces/${workspaceSlug}/projects/${projectId}/external-contours/`, {
      target_project_id: data.target_project_id,
      issue: {
        name: data.name,
        description_html: data.description_html,
        priority: data.priority,
        assignee_ids: data.assignee_ids,
        label_ids: data.label_ids,
        target_date: data.target_date || null,
      },
    })
      .then((response) => response?.data)
      .catch((error) => {
        throw error?.response?.data;
      });
  }

  async decide(
    workspaceSlug: string,
    projectId: string,
    requestId: string,
    action: "accept" | "decline",
    comment?: string
  ): Promise<TExternalContourRequest> {
    return this.post(`/api/workspaces/${workspaceSlug}/projects/${projectId}/external-contours/${requestId}/decision/`, {
      action,
      comment,
    })
      .then((response) => response?.data)
      .catch((error) => {
        throw error?.response?.data;
      });
  }

  async reply(
    workspaceSlug: string,
    projectId: string,
    requestId: string,
    comment: string
  ): Promise<TExternalContourRequest> {
    return this.post(`/api/workspaces/${workspaceSlug}/projects/${projectId}/external-contours/${requestId}/reply/`, {
      comment,
    })
      .then((response) => response?.data)
      .catch((error) => {
        throw error?.response?.data;
      });
  }
}
