/**
 * TMS Permission API Client
 * Updated for src/ backend (TypeScript)
 */

export type Submission = {
  submission_id: string;
  timestamp: string;
  status: string;
  input: { username_list: string[]; ge_input: string };
  results: any[];
  created_at?: string;
  updated_at?: string;
};

export async function fetchSubmissions(limit = 50) {
  // Fetch completed/failed submissions from src/ backend
  const res = await fetch(`/api/submissions?limit=${limit}`);
  if (!res.ok) throw new Error(`Fetch submissions failed: ${res.statusText}`);
  const data = await res.json();
  if (!data.success) throw new Error(data.error || 'API returned error');
  return data.data as Submission[];
}

export async function createSubmission(payload: {
  submission_type: 'tms_permission';
  username_list: string[];
  ge_input: string;
}) {
  const res = await fetch('/api/submit', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(payload),
  });
  if (!res.ok) throw new Error(`Create submission failed: ${res.statusText}`);
  const data = await res.json();
  if (!data.success) throw new Error(data.error || 'API returned error');
  return data.data;
}

export async function getSubmission(submission_id: string) {
  const res = await fetch(`/api/submissions/${encodeURIComponent(submission_id)}`);
  if (!res.ok) throw new Error(`Get submission failed: ${res.statusText}`);
  const data = await res.json();
  if (!data.success) throw new Error(data.error || 'API returned error');
  return data.data;
}

export async function deleteSubmission(submission_id: string) {
  const res = await fetch(`/api/submissions/${encodeURIComponent(submission_id)}`, {
    method: 'DELETE',
  });
  if (!res.ok) throw new Error(`Delete failed: ${res.statusText}`);
  const data = await res.json();
  if (!data.success) throw new Error(data.error || 'API returned error');
  return data.data;
}

export async function retrySubmission(submission_id: string) {
  const res = await fetch(`/api/submissions/${encodeURIComponent(submission_id)}/retry`, {
    method: 'POST',
  });
  if (!res.ok) throw new Error(`Retry failed: ${res.statusText}`);
  const data = await res.json();
  if (!data.success) throw new Error(data.error || 'API returned error');
  return data.data;
}

export async function searchUser(query: string, excludeProjectId?: string) {
  const res = await fetch('/api/user/search', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ query, excludeProjectId }),
  });
  if (!res.ok) throw new Error(`Search user failed: ${res.statusText}`);
  const data = await res.json();
  if (!data.success) throw new Error(data.error || 'API returned error');
  return data.data;
}

export async function getUsernames() {
  const res = await fetch('/api/usernames');
  if (!res.ok) throw new Error(`Get usernames failed: ${res.statusText}`);
  const data = await res.json();
  if (!data.success) throw new Error(data.error || 'API returned error');
  return data.data as string[];
}
