/**
 * Dashboard Service
 *
 * Handles team dashboard data fetching and management
 * - UX Dashboard: Figma files and component tokens
 * - UI Dashboard: Token drift detection and code metrics
 * - QA Dashboard: ESRE definitions and test results
 */

const API_BASE = '/api';

export class DashboardService {
  /**
   * Get aggregated dashboard summary for all teams
   */
  static async getDashboardSummary(projectId) {
    try {
      const response = await fetch(`${API_BASE}/projects/${projectId}/dashboard/summary`);
      if (!response.ok) {
        throw new Error(`Failed to fetch dashboard summary: ${response.statusText}`);
      }
      return await response.json();
    } catch (error) {
      console.error('Error fetching dashboard summary:', error);
      throw error;
    }
  }

  // ============================================
  // UX DASHBOARD - Figma Files
  // ============================================

  /**
   * List all Figma files for a project
   */
  static async listFigmaFiles(projectId) {
    try {
      const response = await fetch(`${API_BASE}/projects/${projectId}/figma-files`);
      if (!response.ok) {
        throw new Error(`Failed to fetch Figma files: ${response.statusText}`);
      }
      return await response.json();
    } catch (error) {
      console.error('Error fetching Figma files:', error);
      return [];
    }
  }

  /**
   * Add a new Figma file to a project
   */
  static async addFigmaFile(projectId, figmaData) {
    try {
      const response = await fetch(`${API_BASE}/projects/${projectId}/figma-files`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(figmaData)
      });
      if (!response.ok) {
        const error = await response.json();
        throw new Error(error.detail || 'Failed to add Figma file');
      }
      return await response.json();
    } catch (error) {
      console.error('Error adding Figma file:', error);
      throw error;
    }
  }

  /**
   * Update Figma file sync status
   */
  static async updateFigmaFileSync(projectId, fileId, syncStatus) {
    try {
      const response = await fetch(`${API_BASE}/projects/${projectId}/figma-files/${fileId}/sync`, {
        method: 'PUT',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          sync_status: syncStatus,
          last_synced: new Date().toISOString()
        })
      });
      if (!response.ok) {
        throw new Error('Failed to update sync status');
      }
      return await response.json();
    } catch (error) {
      console.error('Error updating Figma sync status:', error);
      throw error;
    }
  }

  /**
   * Delete a Figma file
   */
  static async deleteFigmaFile(projectId, fileId) {
    try {
      const response = await fetch(`${API_BASE}/projects/${projectId}/figma-files/${fileId}`, {
        method: 'DELETE'
      });
      if (!response.ok) {
        throw new Error('Failed to delete Figma file');
      }
      return await response.json();
    } catch (error) {
      console.error('Error deleting Figma file:', error);
      throw error;
    }
  }

  // ============================================
  // UI DASHBOARD - Token Drift
  // ============================================

  /**
   * List token drift issues for a project
   */
  static async listTokenDrift(projectId, severity = null) {
    try {
      const url = severity
        ? `${API_BASE}/projects/${projectId}/token-drift?severity=${severity}`
        : `${API_BASE}/projects/${projectId}/token-drift`;

      const response = await fetch(url);
      if (!response.ok) {
        throw new Error(`Failed to fetch token drift: ${response.statusText}`);
      }
      return await response.json();
    } catch (error) {
      console.error('Error fetching token drift:', error);
      return [];
    }
  }

  /**
   * Record a new token drift issue
   */
  static async recordTokenDrift(projectId, driftData) {
    try {
      const response = await fetch(`${API_BASE}/projects/${projectId}/token-drift`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(driftData)
      });
      if (!response.ok) {
        const error = await response.json();
        throw new Error(error.detail || 'Failed to record token drift');
      }
      return await response.json();
    } catch (error) {
      console.error('Error recording token drift:', error);
      throw error;
    }
  }

  /**
   * Update token drift status (pending/fixed/ignored)
   */
  static async updateTokenDriftStatus(projectId, driftId, status) {
    try {
      const response = await fetch(`${API_BASE}/projects/${projectId}/token-drift/${driftId}/status`, {
        method: 'PUT',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ status })
      });
      if (!response.ok) {
        throw new Error('Failed to update token drift status');
      }
      return await response.json();
    } catch (error) {
      console.error('Error updating token drift status:', error);
      throw error;
    }
  }

  // ============================================
  // QA DASHBOARD - ESRE Definitions
  // ============================================

  /**
   * List all ESRE definitions for a project
   */
  static async listESREDefinitions(projectId) {
    try {
      const response = await fetch(`${API_BASE}/projects/${projectId}/esre`);
      if (!response.ok) {
        throw new Error(`Failed to fetch ESRE definitions: ${response.statusText}`);
      }
      return await response.json();
    } catch (error) {
      console.error('Error fetching ESRE definitions:', error);
      return [];
    }
  }

  /**
   * Create a new ESRE definition
   */
  static async createESREDefinition(projectId, esreData) {
    try {
      const response = await fetch(`${API_BASE}/projects/${projectId}/esre`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(esreData)
      });
      if (!response.ok) {
        const error = await response.json();
        throw new Error(error.detail || 'Failed to create ESRE definition');
      }
      return await response.json();
    } catch (error) {
      console.error('Error creating ESRE definition:', error);
      throw error;
    }
  }

  /**
   * Update an ESRE definition
   */
  static async updateESREDefinition(projectId, esreId, updateData) {
    try {
      const response = await fetch(`${API_BASE}/projects/${projectId}/esre/${esreId}`, {
        method: 'PUT',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(updateData)
      });
      if (!response.ok) {
        throw new Error('Failed to update ESRE definition');
      }
      return await response.json();
    } catch (error) {
      console.error('Error updating ESRE definition:', error);
      throw error;
    }
  }

  /**
   * Delete an ESRE definition
   */
  static async deleteESREDefinition(projectId, esreId) {
    try {
      const response = await fetch(`${API_BASE}/projects/${projectId}/esre/${esreId}`, {
        method: 'DELETE'
      });
      if (!response.ok) {
        throw new Error('Failed to delete ESRE definition');
      }
      return await response.json();
    } catch (error) {
      console.error('Error deleting ESRE definition:', error);
      throw error;
    }
  }
}

export default DashboardService;
