import { apiClient } from '@/lib/api/client';
import type { SystemConfig } from '@/types/config';

export interface ContributionSyncStatus {
  isSynced: boolean;
  miningNetworkTotal: string;
  networkTotalContribution: string;
}

export interface MiningStatus {
  initialized: boolean;
  isActive: boolean;
  activatedAt?: string;
  currentEra: number;
  remainingDistribution: string;
  secondDistribution: string;
  accountCount: number;
  totalContribution: string;
  contributionSyncStatus?: ContributionSyncStatus;
  error?: string;
}

export const configsApi = {
  getAll: async (): Promise<SystemConfig[]> => {
    const response = await apiClient.get('/configs');
    return response.data.data;
  },

  update: async (key: string, value: string): Promise<SystemConfig> => {
    const response = await apiClient.put(`/configs/${key}`, { value });
    return response.data.data;
  },

  getTransferEnabled: async (): Promise<boolean> => {
    const response = await apiClient.get('/configs/transfer-enabled');
    return response.data.data.enabled;
  },

  setTransferEnabled: async (enabled: boolean): Promise<void> => {
    await apiClient.post('/configs/transfer-enabled', { enabled });
  },

  getMiningStatus: async (): Promise<MiningStatus> => {
    const response = await apiClient.get('/configs/mining/status');
    return response.data.data;
  },

  activateMining: async (): Promise<{ success: boolean; message: string }> => {
    const response = await apiClient.post('/configs/mining/activate');
    return response.data.data;
  },

  deactivateMining: async (): Promise<{ success: boolean; message: string }> => {
    const response = await apiClient.post('/configs/mining/deactivate');
    return response.data.data;
  },
};
