import { withHeaders } from "@/record-management/services/api/withHeaders";
import axiosInstance from "@/shared/services/axiosInstance";
import {
  PermissionDto,
  PermissionListResponse,
} from "@/user-management/dto/permissions/permissonDto";

import { AxiosResponse } from "axios";
import { PositionQueryParams } from "./positionService";

export interface CreatePermissionDto {
  key: string;
  name: string;
  description?: string;
}

export interface PermissionQueryParams extends PositionQueryParams {
  search?: string;
  name?: string;
  key?: string;
  applicationId?: string;
}

export const permissionService = {
  getAll: (
    params?: PermissionQueryParams,
  ): Promise<AxiosResponse<PermissionListResponse>> =>
    axiosInstance.get("/permissions", { headers: withHeaders(), params }),

  getById: (id: string): Promise<AxiosResponse<PermissionDto>> =>
    axiosInstance.get(`/permissions/${id}`, { headers: withHeaders() }),

  create: (data: CreatePermissionDto): Promise<AxiosResponse<PermissionDto>> =>
    axiosInstance.post("/permissions", data, { headers: withHeaders() }),

  update: (
    id: string,
    data: CreatePermissionDto,
  ): Promise<AxiosResponse<PermissionDto>> =>
    axiosInstance.put(`/permissions/${id}`, data, {
      headers: withHeaders(),
    }),

  delete: (id: string): Promise<AxiosResponse<void>> =>
    axiosInstance.delete(`/permissions/${id}`, { headers: withHeaders() }),
};
