import {
  httpGetRequest,
  httpPostRequest,
  httpPutRequest,
  httpDeleteRequest,
  APIUrlGenerator,
} from "@/utils/baseHttpService";
import { API_ROUTES } from "@/constant/routes";
import {
  Permission,
  CreatePermissionRequest,
  UpdatePermissionRequest,
  PermissionsResponse,
  PermissionResponse,
  CreatePermissionResponse,
  UpdatePermissionResponse,
  DeletePermissionResponse,
  PermissionFilters,
} from "./_models";

export const getPermissions = async (filters?: PermissionFilters) => {
  try {
    const queryParams: Record<string, string | number | null> = {};

    if (filters?.search) queryParams.search = filters.search;
    if (filters?.page) queryParams.page = filters.page;
    if (filters?.limit) queryParams.limit = filters.limit;

    const response = await httpGetRequest<PermissionsResponse>(
      APIUrlGenerator(API_ROUTES.GET_PERMISSIONS, queryParams)
    );

    console.log("Permissions API Response:", response);
    console.log("Permissions data:", response.data);

    // Handle API response structure: {data: {permissions: Permission[] | null}}
    if (
      response.data &&
      response.data.permissions &&
      Array.isArray(response.data.permissions)
    ) {
      return response.data.permissions;
    }

    // If permissions is null or not an array, return empty array
    console.warn("Permissions is null or not an array:", response.data);
    return [];
  } catch (error) {
    console.error("Error fetching permissions:", error);
    return [];
  }
};

export const getPermission = async (id: string) => {
  try {
    const response = await httpGetRequest<PermissionResponse>(
      APIUrlGenerator(API_ROUTES.GET_PERMISSION(id))
    );

    // Handle API response structure
    if (response.data && response.data.permission) {
      return response.data.permission;
    }

    throw new Error("Permission not found");
  } catch (error) {
    console.error("Error fetching permission:", error);
    throw error;
  }
};

export const createPermission = async (
  permissionData: CreatePermissionRequest
) => {
  try {
    const response = await httpPostRequest<CreatePermissionResponse>(
      APIUrlGenerator(API_ROUTES.CREATE_PERMISSION),
      permissionData
    );

    if (response.data && response.data.permission) {
      return response.data.permission;
    }

    throw new Error("Failed to create permission");
  } catch (error) {
    console.error("Error creating permission:", error);
    throw error;
  }
};

export const updatePermission = async (
  id: string,
  permissionData: UpdatePermissionRequest
) => {
  try {
    const response = await httpPutRequest<UpdatePermissionResponse>(
      APIUrlGenerator(API_ROUTES.UPDATE_PERMISSION(id)),
      permissionData
    );

    if (response.data && response.data.permission) {
      return response.data.permission;
    }

    throw new Error("Failed to update permission");
  } catch (error) {
    console.error("Error updating permission:", error);
    throw error;
  }
};

export const deletePermission = async (id: string) => {
  try {
    const response = await httpDeleteRequest<DeletePermissionResponse>(
      APIUrlGenerator(API_ROUTES.DELETE_PERMISSION(id))
    );

    return response.data;
  } catch (error) {
    console.error("Error deleting permission:", error);
    throw error;
  }
};
