import { create } from 'zustand';

// 农事任务接口
export interface FarmingTask {
  id: string;
  title: string;
  description?: string | null;
  status: 'pending' | 'in_progress' | 'completed' | 'cancelled';
  priority: 'low' | 'medium' | 'high';
  assigned_to?: string | null;
  due_date?: string | null;
  created_at: string;
  updated_at: string;
}

// 任务模板接口
export interface TaskTemplate {
  id: string;
  name: string;
  description?: string | null;
  task_type: string;
  estimated_duration?: number | null;
  created_at: string;
}

// 工作流接口
export interface Workflow {
  id: string;
  name: string;
  description?: string | null;
  steps: WorkflowStep[];
  status: 'active' | 'inactive' | 'draft';
  created_at: string;
  updated_at: string;
}

export interface WorkflowStep {
  id: string;
  name: string;
  description?: string | null;
  order: number;
  estimated_duration?: number | null;
}

// Operation state interface
export interface OperationState {
  tasks: FarmingTask[];
  currentTask: FarmingTask | null;
  taskTemplates: TaskTemplate[];
  currentTemplate: TaskTemplate | null;
  workflows: Workflow[];
  currentWorkflow: Workflow | null;

  // Actions
  setTasks: (tasks: FarmingTask[]) => void;
  setCurrentTask: (task: FarmingTask | null) => void;
  setTaskTemplates: (templates: TaskTemplate[]) => void;
  setCurrentTemplate: (template: TaskTemplate | null) => void;
  setWorkflows: (workflows: Workflow[]) => void;
  setCurrentWorkflow: (workflow: Workflow | null) => void;

  // Getters
  getTasks: () => FarmingTask[];
  getCurrentTask: () => FarmingTask | null;
  getTaskTemplates: () => TaskTemplate[];
  getCurrentTemplate: () => TaskTemplate | null;
  getWorkflows: () => Workflow[];
  getCurrentWorkflow: () => Workflow | null;
}

// Create Operation store
export const useOperationStore = create<OperationState>((set, get) => ({
  tasks: [],
  currentTask: null,
  taskTemplates: [],
  currentTemplate: null,
  workflows: [],
  currentWorkflow: null,

  setTasks: (tasks: FarmingTask[]) => {
    set({ tasks });
  },

  setCurrentTask: (task: FarmingTask | null) => {
    set({ currentTask: task });
  },

  setTaskTemplates: (templates: TaskTemplate[]) => {
    set({ taskTemplates: templates });
  },

  setCurrentTemplate: (template: TaskTemplate | null) => {
    set({ currentTemplate: template });
  },

  setWorkflows: (workflows: Workflow[]) => {
    set({ workflows });
  },

  setCurrentWorkflow: (workflow: Workflow | null) => {
    set({ currentWorkflow: workflow });
  },

  getTasks: () => {
    return get().tasks;
  },

  getCurrentTask: () => {
    return get().currentTask;
  },

  getTaskTemplates: () => {
    return get().taskTemplates;
  },

  getCurrentTemplate: () => {
    return get().currentTemplate;
  },

  getWorkflows: () => {
    return get().workflows;
  },

  getCurrentWorkflow: () => {
    return get().currentWorkflow;
  },
}));

export default useOperationStore;