/**
 * Permissions plugin API client.
 *
 * All requests use the shared `apiClient` (`baseURL: '/api/v1'`) and target
 * the Permissions plugin routes under `/permissions/...`.
 */

import { apiDelete, apiGet, apiPost } from './client';

// ─── Types ─────────────────────────────────────────────────────────────────

export type PermissionLevel = 'read' | 'write' | 'admin';

export interface FilePermissionEntry {
  id: string;
  file_id: string;
  user_id: string | null;
  group_id: string | null;
  permission: PermissionLevel;
  user_name?: string | null;
  group_name?: string | null;
  created_at?: string | null;
}

export interface ShareLinkEntry {
  id: string;
  file_id: string;
  token: string;
  url: string;
  password_protected: boolean;
  expires_at: string | null;
  created_at?: string | null;
}

// ─── Payloads ──────────────────────────────────────────────────────────────

export interface GrantPermissionPayload {
  user_id?: string;
  group_id?: string;
  permission: PermissionLevel;
}

export interface CreateShareLinkPayload {
  password?: string;
  expires_at?: string | null;
}

// ─── File Permissions ──────────────────────────────────────────────────────

export function fetchFilePermissions(fileId: string): Promise<FilePermissionEntry[]> {
  return apiGet<FilePermissionEntry[]>(`/permissions/files/${fileId}/permissions`);
}

export function grantPermission(
  fileId: string,
  payload: GrantPermissionPayload
): Promise<FilePermissionEntry> {
  return apiPost<FilePermissionEntry>(`/permissions/files/${fileId}/permissions`, payload);
}

export function revokePermission(fileId: string, userId: string): Promise<void> {
  return apiDelete<void>(`/permissions/files/${fileId}/permissions/${userId}`);
}

// ─── Share Links ───────────────────────────────────────────────────────────

export function createShareLink(
  fileId: string,
  payload: CreateShareLinkPayload
): Promise<ShareLinkEntry> {
  return apiPost<ShareLinkEntry>(`/permissions/files/${fileId}/share-link`, payload);
}

export function revokeShareLink(linkId: string): Promise<void> {
  return apiDelete<void>(`/permissions/share-links/${linkId}`);
}
