import api from "../axios";

export interface UnifiSite {
  id: string;
  name: string;
  siteId: string;
  companyId: string | null;
  company?: {
    id: string;
    name: string;
  };
  createdAt: string;
  updatedAt: string;
}

export interface UnifiSiteOverview {
  health: Array<{
    subsystem: string;
    status: string;
    numAdopted?: number;
    numGateway?: number;
    [key: string]: unknown;
  }>;
  sysInfo: {
    timezone?: string;
    hostname?: string;
    version?: string;
    [key: string]: unknown;
  };
  siteInfo: {
    description?: string;
    name?: string;
    [key: string]: unknown;
  };
}

export interface UnifiDevice {
  id: string;
  mac: string;
  model: string;
  name: string;
  type: string;
  state: string | number;
  ip: string;
  version: string;
  uptime: number;
  radios?: unknown[];
  uplink?: unknown;
  [key: string]: unknown;
}

export interface UnifiWifiNetwork {
  id: string;
  name?: string;
  siteId?: string;
  enabled?: boolean;
  security?: string;
  wpaMode?: string;
  wpaEnc?: string;
  wpa3Support?: boolean;
  wpa3Transition?: boolean;
  wpa3FastRoaming?: boolean;
  wpa3Enhanced192?: boolean;
  passphrase?: string;
  passphraseAutogenerated?: boolean;
  hideSSID?: boolean;
  isGuest?: boolean;
  band?: string;
  bands?: string[];
  networkconfId?: string;
  usergroupId?: string;
  apGroupIds?: string[];
  apGroupMode?: string;
  pmfMode?: string;
  groupRekey?: number;
  dtimMode?: string;
  dtimNg?: number;
  dtimNa?: number;
  dtim6e?: number;
  l2Isolation?: boolean;
  fastRoamingEnabled?: boolean;
  bssTransition?: boolean;
  uapsdEnabled?: boolean;
  iappEnabled?: boolean;
  proxyArp?: boolean;
  mcastenhanceEnabled?: boolean;
  macFilterEnabled?: boolean;
  macFilterPolicy?: string;
  macFilterList?: string[];
  radiusDasEnabled?: boolean;
  radiusMacAuthEnabled?: boolean;
  radiusMacaclFormat?: string;
  minrateSettingPreference?: string;
  minrateNgEnabled?: boolean;
  minrateNgDataRateKbps?: number;
  minrateNgAdvertisingRates?: boolean;
  minrateNaEnabled?: boolean;
  minrateNaDataRateKbps?: number;
  minrateNaAdvertisingRates?: boolean;
  settingPreference?: string;
  no2ghzOui?: boolean;
  privatePreSharedKeysEnabled?: boolean;
  privatePreSharedKeys?: unknown[];
  saeGroups?: unknown[];
  saePsk?: unknown[];
  schedule?: unknown[];
  scheduleWithDuration?: unknown[];
  bcFilterList?: unknown[];
  externalId?: string;
  [key: string]: unknown;
}

export interface UnifiNetwork {
  id: string;
  name: string;
  purpose: string;
  subnet: string;
  vlanId: number | null;
  dhcpEnabled: boolean;
  dhcpStart: string;
  dhcpStop: string;
  domainName: string;
  isNat: boolean;
  enabled: boolean;
}

export interface UnifiWlanGroup {
  id: string;
  name: string;
  siteId: string;
  noDelete: boolean;
  noEdit: boolean;
  hidden: boolean;
}

export interface UnifiApGroup {
  id: string;
  name: string;
  deviceMacs: string[];
  noDelete: boolean;
}

export interface UnifiAccessPoint {
  id: string;
  mac: string;
  model: string;
  type: string;
  name: string;
  state: number;
  adopted: boolean;
  ip: string;
  version: string;
}

export interface UnifiSpeedProfile {
  id: string;
  name: string;
  siteId: string;
  noDelete: boolean;
  downloadLimitKbps: number;
  uploadLimitKbps: number;
}

export interface UnifiPPSK {
  key: string;
  name: string;
  mac: string | null;
  vlanId: number | null;
}

export const unifi = {
  /** Fetch all UniFi sites */
  async fetchSites(accessToken: string) {
    const response = await api.get("/v1/unifi/sites", {
      headers: { Authorization: `Bearer ${accessToken}` },
    });
    return response.data;
  },

  /** Sync sites from UniFi controller */
  async syncSites(accessToken: string) {
    const response = await api.post(
      "/v1/unifi/sites/sync",
      {},
      {
        headers: { Authorization: `Bearer ${accessToken}` },
      },
    );
    return response.data;
  },

  /** Create a new UniFi site */
  async createSite(accessToken: string, description: string) {
    const response = await api.post(
      "/v1/unifi/sites/create",
      { description },
      {
        headers: { Authorization: `Bearer ${accessToken}` },
      },
    );
    return response.data;
  },

  /** Fetch a single UniFi site */
  async fetchSite(accessToken: string, id: string) {
    const response = await api.get(`/v1/unifi/site/${id}`, {
      headers: { Authorization: `Bearer ${accessToken}` },
    });
    return response.data;
  },

  /** Fetch UniFi sites linked to a company */
  async fetchCompanySites(accessToken: string, companyId: string) {
    const response = await api.get(
      `/v1/company/companies/${companyId}/unifi/sites`,
      {
        headers: { Authorization: `Bearer ${accessToken}` },
      },
    );
    return response.data;
  },

  /** Link a site to a company */
  async linkSite(accessToken: string, siteId: string, companyId: string) {
    const response = await api.post(
      `/v1/unifi/site/${siteId}/link`,
      { companyId },
      {
        headers: { Authorization: `Bearer ${accessToken}` },
      },
    );
    return response.data;
  },

  /** Unlink a site from its company */
  async unlinkSite(accessToken: string, siteId: string) {
    const response = await api.post(
      `/v1/unifi/site/${siteId}/unlink`,
      {},
      {
        headers: { Authorization: `Bearer ${accessToken}` },
      },
    );
    return response.data;
  },

  /** Get site overview (health, sysInfo, siteInfo) */
  async fetchSiteOverview(accessToken: string, siteId: string) {
    const response = await api.get(`/v1/unifi/site/${siteId}/overview`, {
      headers: { Authorization: `Bearer ${accessToken}` },
    });
    return response.data;
  },

  /** Get site devices */
  async fetchSiteDevices(accessToken: string, siteId: string) {
    const response = await api.get(`/v1/unifi/site/${siteId}/devices`, {
      headers: { Authorization: `Bearer ${accessToken}` },
    });
    return response.data;
  },

  /** Get site WiFi networks */
  async fetchSiteWifi(accessToken: string, siteId: string) {
    const response = await api.get(`/v1/unifi/site/${siteId}/wifi`, {
      headers: { Authorization: `Bearer ${accessToken}` },
    });
    return response.data;
  },

  /** Update a WiFi network */
  async updateWifi(
    accessToken: string,
    siteId: string,
    wlanId: string,
    data: Record<string, unknown>,
  ) {
    const response = await api.patch(
      `/v1/unifi/site/${siteId}/wifi/${wlanId}`,
      data,
      {
        headers: { Authorization: `Bearer ${accessToken}` },
      },
    );
    return response.data;
  },

  /** Get site networks */
  async fetchSiteNetworks(accessToken: string, siteId: string) {
    const response = await api.get(`/v1/unifi/site/${siteId}/networks`, {
      headers: { Authorization: `Bearer ${accessToken}` },
    });
    return response.data;
  },

  /** Get WLAN groups */
  async fetchWlanGroups(accessToken: string, siteId: string) {
    const response = await api.get(`/v1/unifi/site/${siteId}/wlan-groups`, {
      headers: { Authorization: `Bearer ${accessToken}` },
    });
    return response.data;
  },

  /** Get AP groups (collections of access points for broadcasting) */
  async fetchApGroups(accessToken: string, siteId: string) {
    const response = await api.get(`/v1/unifi/site/${siteId}/ap-groups`, {
      headers: { Authorization: `Bearer ${accessToken}` },
    });
    return response.data;
  },

  /** Get access points */
  async fetchAccessPoints(accessToken: string, siteId: string) {
    const response = await api.get(`/v1/unifi/site/${siteId}/access-points`, {
      headers: { Authorization: `Bearer ${accessToken}` },
    });
    return response.data;
  },

  /** Get speed profiles (user groups) */
  async fetchSpeedProfiles(accessToken: string, siteId: string) {
    const response = await api.get(`/v1/unifi/site/${siteId}/speed-profiles`, {
      headers: { Authorization: `Bearer ${accessToken}` },
    });
    return response.data;
  },

  /** Create a speed profile */
  async createSpeedProfile(
    accessToken: string,
    siteId: string,
    data: {
      name: string;
      downloadLimitKbps?: number;
      uploadLimitKbps?: number;
    },
  ) {
    const response = await api.post(
      `/v1/unifi/site/${siteId}/speed-profiles`,
      data,
      {
        headers: { Authorization: `Bearer ${accessToken}` },
      },
    );
    return response.data;
  },

  /** Get private PSKs for a WLAN */
  async fetchPPSKs(accessToken: string, siteId: string, wlanId: string) {
    const response = await api.get(
      `/v1/unifi/site/${siteId}/wifi/${wlanId}/ppsk`,
      {
        headers: { Authorization: `Bearer ${accessToken}` },
      },
    );
    return response.data;
  },

  /** Create a private PSK on a WLAN */
  async createPPSK(
    accessToken: string,
    siteId: string,
    wlanId: string,
    data: { key: string; name: string; mac?: string; vlanId?: number },
  ) {
    const response = await api.post(
      `/v1/unifi/site/${siteId}/wifi/${wlanId}/ppsk`,
      data,
      {
        headers: { Authorization: `Bearer ${accessToken}` },
      },
    );
    return response.data;
  },

  /** Get WiFi limits per AP per radio */
  async fetchWifiLimits(accessToken: string, siteId: string) {
    const response = await api.get(`/v1/unifi/site/${siteId}/wifi-limits`, {
      headers: { Authorization: `Bearer ${accessToken}` },
    });
    return response.data;
  },
};
