import { apiRequest } from "./apiClient";

import type { NetworkTrafficResponse, VpsHealth } from "@/types/api";

export type ControllerClient = {
  id: string;
  name: string;
  file: string;
  path: string;
  controller_count: number;
  modified_at: string;
};

export type ControllerClientsResponse = {
  clients: ControllerClient[];
};

export type ControllerClientFileResponse = {
  content: string;
};

export type RouterFirewallRule = {
  id: string;
  name: string;
  proto: string;
  srcZone: string;
  destZone: string;
  externalPort: string;
  internalIp: string;
  internalPort: string;
  target: string;
};

export type RouterFirewallRulesResponse = {
  ok: boolean;
  routerHost: string;
  rules: RouterFirewallRule[];
  fetchedAt: string;
};

export const vpsApi = {
  health: () => apiRequest<VpsHealth>("/api/vps/health"),

  networkTraffic: () =>
    apiRequest<NetworkTrafficResponse>("/api/vps/network-traffic"),

  udp2rawTraffic: () =>
    apiRequest<NetworkTrafficResponse>("/api/vps/udp2raw-traffic"),

  rollbackLastBackup: () =>
    apiRequest<{ restored: boolean }>(
      "/api/vps/wireguard/rollback-last-backup",
      {
        method: "POST",
      },
    ),

  // ALL clients
  listControllerClients: () =>
    apiRequest<ControllerClientsResponse>("/api/vps/controllers/clients"),

  // ONE client's .txt content
  readControllerClient: (clientId: string) =>
    apiRequest<ControllerClientFileResponse>(
      `/api/vps/controllers/clients/${encodeURIComponent(clientId)}`,
    ),

  updateControllerClient: (clientId: string, content: string) =>
    apiRequest<ControllerClientFileResponse>(
      `/api/vps/controllers/clients/${encodeURIComponent(clientId)}`,
      {
        method: "POST",
        body: JSON.stringify({ content }),
      },
    ),

  createControllerClient: (name: string, password: string) =>
    apiRequest<ControllerClientFileResponse>(`/api/vps/controllers/clients`, {
      method: "POST",
      body: JSON.stringify({
        name,
        password,
      }),
    }),

  deleteControllerClient: (clientId: string) =>
    apiRequest<ControllerClientFileResponse>(`/api/vps/controllers/clients`, {
      method: "DELETE",
      body: JSON.stringify({ clientId }),
    }),

  getRouterFirewallRules: (routerHost: string, routerPassword: string) =>
    apiRequest<RouterFirewallRulesResponse>(
      "/api/vps/controllers/routers/firewall/rules",
      {
        method: "POST",
        body: JSON.stringify({
          routerHost,
          routerPassword,
        }),
      },
    ),

  createRouterFirewallRule: (
    routerHost: string,
    routerPassword: string,
    rule: RouterFirewallRule,
  ) =>
    apiRequest<{
      ok: boolean;
      routerHost: string;
      rule: RouterFirewallRule;
      deletedId: string | null;
      fetchedAt: string;
    }>("/api/vps/controllers/routers/firewall/rules/create", {
      method: "POST",
      body: JSON.stringify({
        routerHost,
        routerPassword,
        ...rule,
      }),
    }),

  updateRouterFirewallRule: (
    routerHost: string,
    routerPassword: string,
    rule: RouterFirewallRule,
  ) =>
    apiRequest<{
      ok: boolean;
      routerHost: string;
      rule: RouterFirewallRule;
      deletedId: string | null;
      fetchedAt: string;
    }>("/api/vps/controllers/routers/firewall/rules/update", {
      method: "POST",
      body: JSON.stringify({
        routerHost,
        routerPassword,
        ...rule,
      }),
    }),

  deleteRouterFirewallRule: (
    routerHost: string,
    routerPassword: string,
    id: string,
  ) =>
    apiRequest<{
      ok: boolean;
      routerHost: string;
      rule: null;
      deletedId: string;
      fetchedAt: string;
    }>("/api/vps/controllers/routers/firewall/rules", {
      method: "DELETE",
      body: JSON.stringify({
        routerHost,
        routerPassword,
        id,
      }),
    }),
};
