// src/client/shared/stores/headerActions.ts
// Global store for ActionBar state - single source of truth for header actions

import { ref, computed, type Component } from 'vue';
import { defineStore } from 'pinia';

export interface CustomAction {
  id: string;
  label?: string;
  icon?: string | Component;
  onClick: () => void;
  variant?: 'primary' | 'success' | 'danger' | 'default';
  visible?: boolean;
  dataActionId?: string;
}

export interface HeaderHandlers {
  onSearch?: ((query: string) => void) | null;
  onExport?: (() => void) | null;
  onPrint?: (() => void) | null;
  onCreate?: (() => void) | null;
  onArchive?: (() => void) | null;
  onDatePicker?: ((date: string) => void) | null;
  showSearch?: boolean;
  showExport?: boolean;
  showPrint?: boolean;
  showCreate?: boolean;
  showArchive?: boolean;
}

export const useHeaderActionsStore = defineStore('headerActions', () => {
  // Header title
  const title = ref<string>('');

  // Search handler
  const onSearch = ref<((query: string) => void) | null>(null);

  // Export/Print handler
  const onExport = ref<(() => void) | null>(null);

  // Print handler
  const onPrint = ref<(() => void) | null>(null);

  // Create handler (for global create button)
  const onCreate = ref<(() => void) | null>(null);

  // Archive handler (for global archive button)
  const onArchive = ref<(() => void) | null>(null);

  // Date picker handler
  const onDatePicker = ref<((date: string) => void) | null>(null);

  // Visibility flags
  const showSearch = ref(true);
  const showExport = ref(true);
  const showPrint = ref(true);
  const showCreate = ref(false);
  const showArchive = ref(false);

  // Custom actions (for page-specific controls)
  const customActions = ref<CustomAction[]>([]);

  // Computed: Check if any actions are available
  const hasActions = computed(() => showSearch.value || showExport.value || customActions.value.length > 0);

  // Set header title
  const setTitle = (newTitle: string) => {
    title.value = newTitle;
  };

  // Set all handlers at once
  const setHandlers = (handlers: HeaderHandlers) => {
    if (handlers.onSearch !== undefined) {
      onSearch.value = handlers.onSearch;
    }
    if (handlers.onExport !== undefined) {
      onExport.value = handlers.onExport;
    }
    if (handlers.onPrint !== undefined) {
      onPrint.value = handlers.onPrint;
    }
    if (handlers.onCreate !== undefined) {
      onCreate.value = handlers.onCreate;
    }
    if (handlers.showSearch !== undefined) {
      showSearch.value = handlers.showSearch;
    }
    if (handlers.showExport !== undefined) {
      showExport.value = handlers.showExport;
    }
    if (handlers.showPrint !== undefined) {
      showPrint.value = handlers.showPrint;
    }
    if (handlers.showCreate !== undefined) {
      showCreate.value = handlers.showCreate;
    }
  };

  // Set custom actions
  const setCustomActions = (actions: CustomAction[]) => {
    customActions.value = actions;
  };

  // Add a custom action
  const addCustomAction = (action: CustomAction) => {
    customActions.value.push(action);
  };

  // Remove a custom action by id
  const removeCustomAction = (id: string) => {
    customActions.value = customActions.value.filter(a => a.id !== id);
  };

  // Reset all handlers (for cleanup when leaving a page)
  const reset = () => {
    title.value = '';
    onSearch.value = null;
    onExport.value = null;
    onPrint.value = null;
    onCreate.value = null;
    onArchive.value = null;
    onDatePicker.value = null;
    showSearch.value = true; // Permanent tool, defaults to true
    showExport.value = true; // Permanent tool, defaults to true
    showPrint.value = true; // Permanent tool, defaults to true
    showCreate.value = false;
    showArchive.value = false;
    customActions.value = [];
  };

  // Execute search if handler is available
  const executeSearch = (query: string) => {
    if (onSearch.value) {
      onSearch.value(query);
    }
  };

  // Execute export if handler is available
  const executeExport = () => {
    if (onExport.value) {
      onExport.value();
    }
  };

  // Execute create if handler is available
  const executeCreate = () => {
    if (onCreate.value) {
      onCreate.value();
    }
  };

  // Execute archive if handler is available
  const executeArchive = () => {
    if (onArchive.value) {
      onArchive.value();
    }
  };

  // Execute date picker change if handler is available
  const executeDatePicker = (date: string) => {
    if (onDatePicker.value) {
      onDatePicker.value(date);
    }
  };

  // Execute print if handler is available, otherwise use window.print()
  const executePrint = () => {
    if (onPrint.value) {
      onPrint.value();
    } else {
      window.print();
    }
  };

  return {
    title,
    onSearch,
    onExport,
    onPrint,
    onCreate,
    onArchive,
    onDatePicker,
    showSearch,
    showExport,
    showPrint,
    showCreate,
    showArchive,
    customActions,
    hasActions,
    setTitle,
    setHandlers,
    setCustomActions,
    addCustomAction,
    removeCustomAction,
    reset,
    executeSearch,
    executeExport,
    executePrint,
    executeCreate,
    executeArchive,
    executeDatePicker,
  };
});
