import { create } from 'zustand'
import {
  AppVersion,
  CreateVersionInput,
  UpdateVersionInput,
  UploadVersionInput,
  Platform,
} from '@/domain'
import { versionRepository } from '@/infrastructure'

interface VersionState {
  versions: AppVersion[]
  selectedVersion: AppVersion | null
  isLoading: boolean
  error: string | null
  filter: {
    platform?: Platform
    includeDisabled: boolean
  }

  // Actions
  fetchVersions: () => Promise<void>
  fetchVersionById: (id: string) => Promise<void>
  createVersion: (input: CreateVersionInput) => Promise<AppVersion>
  updateVersion: (id: string, input: UpdateVersionInput) => Promise<AppVersion>
  deleteVersion: (id: string) => Promise<void>
  toggleVersion: (id: string, isEnabled: boolean) => Promise<void>
  uploadVersion: (input: UploadVersionInput) => Promise<AppVersion>
  setFilter: (filter: Partial<VersionState['filter']>) => void
  setSelectedVersion: (version: AppVersion | null) => void
  clearError: () => void
}

export const useVersionStore = create<VersionState>((set, get) => ({
  versions: [],
  selectedVersion: null,
  isLoading: false,
  error: null,
  filter: {
    platform: undefined,
    includeDisabled: true,
  },

  fetchVersions: async () => {
    set({ isLoading: true, error: null })
    try {
      const { filter } = get()
      const versions = await versionRepository.list(filter)
      set({ versions, isLoading: false })
    } catch (err) {
      set({
        error: err instanceof Error ? err.message : 'Failed to fetch versions',
        isLoading: false,
      })
    }
  },

  fetchVersionById: async (id: string) => {
    set({ isLoading: true, error: null })
    try {
      const version = await versionRepository.getById(id)
      set({ selectedVersion: version, isLoading: false })
    } catch (err) {
      set({
        error: err instanceof Error ? err.message : 'Failed to fetch version',
        isLoading: false,
      })
    }
  },

  createVersion: async (input: CreateVersionInput) => {
    set({ isLoading: true, error: null })
    try {
      const version = await versionRepository.create(input)
      const { versions } = get()
      set({ versions: [version, ...versions], isLoading: false })
      return version
    } catch (err) {
      set({
        error: err instanceof Error ? err.message : 'Failed to create version',
        isLoading: false,
      })
      throw err
    }
  },

  updateVersion: async (id: string, input: UpdateVersionInput) => {
    set({ isLoading: true, error: null })
    try {
      const version = await versionRepository.update(id, input)
      const { versions } = get()
      set({
        versions: versions.map((v) => (v.id === id ? version : v)),
        selectedVersion: version,
        isLoading: false,
      })
      return version
    } catch (err) {
      set({
        error: err instanceof Error ? err.message : 'Failed to update version',
        isLoading: false,
      })
      throw err
    }
  },

  deleteVersion: async (id: string) => {
    set({ isLoading: true, error: null })
    try {
      await versionRepository.delete(id)
      const { versions, selectedVersion } = get()
      set({
        versions: versions.filter((v) => v.id !== id),
        selectedVersion: selectedVersion?.id === id ? null : selectedVersion,
        isLoading: false,
      })
    } catch (err) {
      set({
        error: err instanceof Error ? err.message : 'Failed to delete version',
        isLoading: false,
      })
      throw err
    }
  },

  toggleVersion: async (id: string, isEnabled: boolean) => {
    set({ isLoading: true, error: null })
    try {
      await versionRepository.toggle(id, isEnabled)
      const { versions } = get()
      set({
        versions: versions.map((v) =>
          v.id === id ? { ...v, isEnabled } : v
        ),
        isLoading: false,
      })
    } catch (err) {
      set({
        error: err instanceof Error ? err.message : 'Failed to toggle version',
        isLoading: false,
      })
      throw err
    }
  },

  uploadVersion: async (input: UploadVersionInput) => {
    set({ isLoading: true, error: null })
    try {
      const version = await versionRepository.upload(input)
      const { versions } = get()
      set({ versions: [version, ...versions], isLoading: false })
      return version
    } catch (err) {
      set({
        error: err instanceof Error ? err.message : 'Failed to upload version',
        isLoading: false,
      })
      throw err
    }
  },

  setFilter: (filter) => {
    set((state) => ({
      filter: { ...state.filter, ...filter },
    }))
  },

  setSelectedVersion: (version) => {
    set({ selectedVersion: version })
  },

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