import type { ApiSettings } from "../types";
import type { PlatformModuleInfo, PlatformPublicModuleInfo } from "../types";
import { apiFetch } from "./client";

export type PlatformModulesResponse = { modules: PlatformModuleInfo[] };
export type PlatformPublicModulesResponse = { modules: PlatformPublicModuleInfo[] };

export type PlatformStatusResponse = {
  maintenance_mode: {
    enabled: boolean;
    message?: string | null;
  };
  i18n?: {
    available_languages: Array<{
      code: string;
      label: string;
      native_label?: string | null;
    }>;
    enabled_languages: string[];
    default_language: string;
  };
};

export type PlatformPermission = {
  scope: string;
  module_id: string;
  resource: string;
  action: string;
  label: string;
  description: string;
  category: string;
  level: "system" | "tenant";
  deprecated: boolean;
};

export type PlatformPermissionsResponse = { permissions: PlatformPermission[] };

export async function fetchPlatformModules(settings: ApiSettings): Promise<PlatformModulesResponse> {
  return apiFetch<PlatformModulesResponse>(settings, "/api/v1/platform/modules");
}

export async function fetchPlatformPublicModules(settings: ApiSettings): Promise<PlatformPublicModulesResponse> {
  return apiFetch<PlatformPublicModulesResponse>(settings, "/api/v1/platform/public-modules");
}

export async function fetchPlatformStatus(settings: ApiSettings): Promise<PlatformStatusResponse> {
  return apiFetch<PlatformStatusResponse>(settings, "/api/v1/platform/status");
}

export async function fetchPlatformPermissions(settings: ApiSettings): Promise<PlatformPermissionsResponse> {
  return apiFetch<PlatformPermissionsResponse>(settings, "/api/v1/platform/permissions");
}
