/**
 * GitHub Store - GitHub integration state management
 */

import { create } from 'zustand';
import * as githubAPI from '../api/github';

interface GitHubState {
  // State
  selectedProjectId: string | null;
  isLoading: boolean;
  isImporting?: boolean;  // Alias for isLoading
  error: string | null;
  syncStatus: any | null;
  syncStatuses?: Map<string, any>;  // Per-project sync statuses
  isConnected: boolean;

  // Actions
  setSelectedProject: (projectId: string | null) => void;
  importRepository: (url: string, projectName?: string) => Promise<void>;
  pullFromGithub: (projectId: string) => Promise<void>;
  pullChanges?: (projectId: string) => Promise<void>;  // Alias
  pushToGithub: (projectId: string, message?: string) => Promise<void>;
  pushChanges?: (projectId: string, message?: string) => Promise<void>;  // Alias
  syncWithGithub: (projectId: string) => Promise<void>;
  syncProject?: (projectId: string) => Promise<void>;  // Alias
  getStatus: (projectId: string) => Promise<void>;
  getSyncStatus?: (projectId: string) => Promise<void>;  // Alias
  disconnect: (projectId: string) => Promise<void>;
  clearError: () => void;
}

export const useGithubStore = create<GitHubState>((set, get) => ({
  selectedProjectId: null,
  isLoading: false,
  error: null,
  syncStatus: null,
  syncStatuses: new Map(),
  isConnected: false,

  setSelectedProject: (projectId: string | null) => {
    set({ selectedProjectId: projectId });
  },

  importRepository: async (url: string, projectName?: string) => {
    set({ isLoading: true, error: null });
    try {
      const result = await githubAPI.importRepository({ url, project_name: projectName });
      set({
        isLoading: false,
        isConnected: true,
      });
    } catch (error) {
      const message = error instanceof Error ? error.message : 'Failed to import repository';
      set({ error: message, isLoading: false });
    }
  },

  pullFromGithub: async (projectId: string) => {
    set({ isLoading: true, error: null });
    try {
      await githubAPI.pullFromGithub(projectId);
      set({ isLoading: false });
    } catch (error) {
      const message = error instanceof Error ? error.message : 'Failed to pull from GitHub';
      set({ error: message, isLoading: false });
    }
  },

  pushToGithub: async (projectId: string, message?: string) => {
    set({ isLoading: true, error: null });
    try {
      await githubAPI.pushToGithub(projectId, message);
      set({ isLoading: false });
    } catch (error) {
      const message = error instanceof Error ? error.message : 'Failed to push to GitHub';
      set({ error: message, isLoading: false });
    }
  },

  syncWithGithub: async (projectId: string) => {
    set({ isLoading: true, error: null });
    try {
      await githubAPI.syncWithGithub(projectId);
      set({ isLoading: false });
    } catch (error) {
      const message = error instanceof Error ? error.message : 'Failed to sync with GitHub';
      set({ error: message, isLoading: false });
    }
  },

  getStatus: async (projectId: string) => {
    set({ isLoading: true, error: null });
    try {
      const status = await githubAPI.getGithubStatus(projectId);
      const syncStatuses = new Map(get().syncStatuses);
      syncStatuses.set(projectId, status);
      set({
        syncStatus: status,
        syncStatuses,
        isLoading: false,
        isConnected: status.status === 'synced',
      });
    } catch (error) {
      const message = error instanceof Error ? error.message : 'Failed to get status';
      set({ error: message, isLoading: false });
    }
  },

  // Aliases for API compatibility
  getSyncStatus: async (projectId: string) => {
    return get().getStatus(projectId);
  },

  pullChanges: async (projectId: string) => {
    return get().pullFromGithub(projectId);
  },

  pushChanges: async (projectId: string) => {
    return get().pushToGithub(projectId);
  },

  syncProject: async (projectId: string) => {
    return get().syncWithGithub(projectId);
  },

  disconnect: async (projectId: string) => {
    set({ isLoading: true, error: null });
    try {
      await githubAPI.disconnectGithub(projectId);
      set({
        isLoading: false,
        isConnected: false,
        syncStatus: null,
      });
    } catch (error) {
      const message = error instanceof Error ? error.message : 'Failed to disconnect';
      set({ error: message, isLoading: false });
    }
  },

  clearError: () => {
    set({ error: null });
  },
}));
