export default function useWorkOrder() {
  class MWorkOrder implements TWorkOrder {
    _id?: string;
    description: string;
    service: string;
    subject: string | null;
    provider: string | null;
    providerName?: string;
    status?: string;
    assignee?: string | TUser;
    assigneeName?: string;
    assigneeInfo?: Record<string, any>;
    attachments?: string[];
    createdBy?: string | TUser;
    createdByName?: string;
    site: string;
    organization: string;
    location?: string;
    comments?: TComment[];
    createdAt?: string | Date;
    updatedAt?: string | Date;
    feedback?: string;
    statusUpdates?: any[];
    prefix?: string;
    noOfDigits?: any;
    number?: number;
    version?: number;
    isHighPriority?: boolean;

    constructor(workOrder: Partial<TWorkOrder> = {}) {
      this._id = workOrder._id || "";
      this.description = workOrder.description || "";
      this.service = workOrder.service || "";
      this.subject = workOrder.subject || null;
      this.provider = workOrder.provider || null;
      this.providerName = workOrder.providerName || "";
      this.status = workOrder.status || "";
      this.assignee = workOrder.assignee;
      this.attachments = workOrder.attachments || [];
      this.createdBy = workOrder.createdBy || "";
      this.createdByName = workOrder.createdByName;
      this.site = workOrder.site || "";
      this.organization = workOrder.organization || "";
      this.location = workOrder.location || "";
      this.assigneeName = workOrder.assigneeName || "";
      this.comments = workOrder.comments || [];
      this.createdAt = "";
      this.updatedAt = "";
      this.feedback = workOrder.feedback || "";
      this.prefix = workOrder.prefix || "";
      this.noOfDigits = workOrder.noOfDigits || 1;
      this.number = workOrder.number || 1;
      this.version = workOrder.version || 0;
      this.isHighPriority = workOrder.isHighPriority || false;
    }
  }

  const workOrder = useState("workOrder", () => new MWorkOrder());
  const workOrders = useState<Array<TWorkOrder>>("workOrders", () => []);
  const page = useState("page", () => 1);
  const pages = useState("pages", () => 0);
  const pageRange = useState("pageRange", () => "-- - -- of --");

  function setWorkOrder(data?: TWorkOrder) {
    workOrder.value = data || new MWorkOrder();
  }

  async function getWorkOrders({
    page = 1,
    limit = 10,
    organization,
    provider,
    providerService,
    site,
    status,
    startDate = "",
    endDate = "",
    date,
    search = "",
  }: {
    page?: number;
    limit?: number;
    search?: string;
    startDate?: any;
    endDate?: any;
    date?: any;
    status?: string;
    organization?: string;
    site?: string;
    provider?: string | any;
    providerService?: string;
    service?: string;
  } = {}) {
    return useNuxtApp().$api<Record<string, any>[]>("/api/work-orders2/v1", {
      method: "GET",
      query: {
        page,
        limit,
        search,
        status,
        startDate,
        endDate,
        date,
        organization,
        site,
        provider,
        providerService,
      },
    });
  }

  function getWorkOrderById(id: string) {
    return useNuxtApp().$api<TWorkOrder>(`/api/work-orders2/v1/id/${id}`, {
      method: "GET",
    });
  }

  function createWorkOrder(payload: any) {
    return useNuxtApp().$api<Record<string, any>>("/api/work-orders2/v1", {
      method: "POST",
      body: payload,
    });
  }

  function updateStatusWorkOrder(payload: any) {
    return useNuxtApp().$api("/api/work-orders2/status-update", {
      method: "POST",
      body: payload,
    });
  }

  function updateWorkOrder(id: string, payload: object) {
    return useNuxtApp().$api<Record<string, any>>(`/api/work-orders/${id}`, {
      method: "PUT",
      body: payload,
    });
  }

  function deleteWorkOrder(id: string) {
    return useNuxtApp().$api<Record<string, any>>(`/api/work-orders/${id}`, {
      method: "DELETE",
    });
  }

  // function deleteWorkOrder(id: string) {
  //   return useNuxtApp().$api<Record<string, any>>(
  //     `/api/work-orders/deleted/work-order/${id}`,
  //     {
  //       method: "PUT",
  //       // query: { id },
  //     }
  //   );
  // }

  function getWorkOrderSettings(_workOrderSettings: Record<string, any>) {
    return useNuxtApp().$api<Record<string, any>>(
      "/api/work-order-settings/get-by-site",
      {
        method: "POST",
        body: _workOrderSettings,
      }
    );
  }

  function createWorkOrderSettings(_workOrderSettings: Record<string, any>) {
    return useNuxtApp().$api<Record<string, any>>("/api/work-order-settings", {
      method: "POST",
      body: _workOrderSettings,
    });
  }

  return {
    workOrders,
    setWorkOrder,
    workOrder,
    page,
    pages,
    pageRange,
    createWorkOrder,
    updateStatusWorkOrder,
    getWorkOrders,
    getWorkOrderById,
    updateWorkOrder,
    deleteWorkOrder,
    getWorkOrderSettings,
    createWorkOrderSettings,
  };
}
