import { create } from 'zustand';

export interface PhotoEdit {
  id: string;
  order: number;
  toolType: string;
  toolLabel: string;
  prompt: string;
  originalPath: string;
  editedPath: string | null;
  analysis: string | null;
  createdAt: string;
}

export interface PhotoProject {
  id: string;
  title: string;
  status: string;
  createdAt: string;
  edits: PhotoEdit[];
}

export type AppView = 'home' | 'editor' | 'gallery' | 'generate';

interface AppState {
  // Navigation
  currentView: AppView;
  setView: (view: AppView) => void;

  // Current project
  currentProject: PhotoProject | null;
  setCurrentProject: (project: PhotoProject | null) => void;

  // Original image
  originalImage: string | null; // base64 or URL
  setOriginalImage: (image: string | null) => void;
  originalFileName: string | null;
  setOriginalFileName: (name: string | null) => void;

  // Analysis
  analysis: string | null;
  setAnalysis: (analysis: string | null) => void;

  // Editing
  isEditing: boolean;
  setIsEditing: (editing: boolean) => void;
  editProgress: number;
  setEditProgress: (progress: number) => void;
  editError: string | null;
  setEditError: (error: string | null) => void;

  // Comparison
  editedImage: string | null;
  setEditedImage: (image: string | null) => void;
  sliderPosition: number;
  setSliderPosition: (pos: number) => void;

  // Gallery
  projects: PhotoProject[];
  setProjects: (projects: PhotoProject[]) => void;

  // Custom prompt
  customPrompt: string;
  setCustomPrompt: (prompt: string) => void;

  // Reset
  resetEditor: () => void;
}

export const useAppStore = create<AppState>((set) => ({
  // Navigation
  currentView: 'home',
  setView: (view) => set({ currentView: view }),

  // Current project
  currentProject: null,
  setCurrentProject: (project) => set({ currentProject: project }),

  // Original image
  originalImage: null,
  setOriginalImage: (image) => set({ originalImage: image }),
  originalFileName: null,
  setOriginalFileName: (name) => set({ originalFileName: name }),

  // Analysis
  analysis: null,
  setAnalysis: (analysis) => set({ analysis }),

  // Editing
  isEditing: false,
  setIsEditing: (editing) => set({ isEditing: editing }),
  editProgress: 0,
  setEditProgress: (progress) => set({ editProgress: progress }),
  editError: null,
  setEditError: (error) => set({ editError: error }),

  // Comparison
  editedImage: null,
  setEditedImage: (image) => set({ editedImage: image }),
  sliderPosition: 50,
  setSliderPosition: (pos) => set({ sliderPosition: pos }),

  // Gallery
  projects: [],
  setProjects: (projects) => set({ projects }),

  // Custom prompt
  customPrompt: '',
  setCustomPrompt: (prompt) => set({ customPrompt: prompt }),

  // Reset
  resetEditor: () =>
    set({
      originalImage: null,
      originalFileName: null,
      analysis: null,
      isEditing: false,
      editProgress: 0,
      editError: null,
      editedImage: null,
      sliderPosition: 50,
      customPrompt: '',
      currentProject: null,
    }),
}));