import { create } from 'zustand'
import type { Channel, IpInfo, ErrorLogEntry, AppSettings, MonitoringResult } from '@shared/types'

interface AppState {
  // IP Info
  ipInfo: IpInfo | null
  ipLoading: boolean
  setIpInfo: (info: IpInfo | null) => void
  setIpLoading: (loading: boolean) => void

  // Channels
  channels: Channel[]
  channelsLoading: boolean
  setChannels: (channels: Channel[]) => void
  updateChannel: (channel: Channel) => void
  setChannelsLoading: (loading: boolean) => void

  // Monitoring
  isMonitoring: boolean
  monitoringResults: Map<string, MonitoringResult>
  setIsMonitoring: (running: boolean) => void
  updateMonitoringResult: (result: MonitoringResult) => void
  clearMonitoringResults: () => void

  // Error Log
  errorLog: ErrorLogEntry[]
  addErrorLog: (entry: ErrorLogEntry) => void
  setErrorLog: (log: ErrorLogEntry[]) => void
  clearErrorLog: () => void

  // Settings
  settings: AppSettings | null
  setSettings: (settings: AppSettings) => void

  // UI State
  sidebarCollapsed: boolean
  toggleSidebar: () => void
}

const defaultSettings: AppSettings = {
  general: {
    startMinimized: false,
    minimizeToTray: true,
    autoStart: false,
    language: 'tr',
  },
  monitoring: {
    pingInterval: 30,
    streamCheckInterval: 60,
    timeout: 5000,
    retryCount: 2,
  },
  notifications: {
    enabled: true,
    windows: { enabled: true },
    telegram: {
      enabled: false,
      botToken: '',
      chatId: '',
    },
    email: {
      enabled: false,
      smtpHost: '',
      smtpPort: 587,
      smtpUser: '',
      smtpPass: '',
      fromEmail: '',
      toEmail: '',
    },
    autoNotify: true,
    notifyOnPingFail: true,
    notifyOnStreamError: true,
    notifyOn404: true,
    notifyOnConnReset: true,
  },
  panel: {
    apiUrl: '',
    authToken: '',
    monitorId: '',
    enabled: false,
  },
}

export const useStore = create<AppState>((set) => ({
  // IP Info
  ipInfo: null,
  ipLoading: false,
  setIpInfo: (info) => set({ ipInfo: info }),
  setIpLoading: (loading) => set({ ipLoading: loading }),

  // Channels
  channels: [],
  channelsLoading: false,
  setChannels: (channels) => set({ channels }),
  updateChannel: (channel) =>
    set((state) => ({
      channels: state.channels.map((c) =>
        c.id === channel.id ? channel : c
      ),
    })),
  setChannelsLoading: (loading) => set({ channelsLoading: loading }),

  // Monitoring
  isMonitoring: false,
  monitoringResults: new Map(),
  setIsMonitoring: (running) => set({ isMonitoring: running }),
  updateMonitoringResult: (result) =>
    set((state) => {
      const newResults = new Map(state.monitoringResults)
      newResults.set(result.channelId, result)
      return { monitoringResults: newResults }
    }),
  clearMonitoringResults: () => set({ monitoringResults: new Map() }),

  // Error Log
  errorLog: [],
  addErrorLog: (entry) =>
    set((state) => ({
      errorLog: [entry, ...state.errorLog].slice(0, 100), // Keep last 100
    })),
  setErrorLog: (log) => set({ errorLog: log }),
  clearErrorLog: () => set({ errorLog: [] }),

  // Settings
  settings: defaultSettings,
  setSettings: (settings) => set({ settings }),

  // UI State
  sidebarCollapsed: false,
  toggleSidebar: () =>
    set((state) => ({ sidebarCollapsed: !state.sidebarCollapsed })),
}))

