import { create } from 'zustand';
import type { Batch, BatchFormData } from '@/types';

interface BatchState {
  batches: Batch[];
  selectedBatch: Batch | null;
  fetchBatches: () => Promise<void>;
  createBatch: (data: BatchFormData) => Promise<void>;
  updateBatch: (id: number, data: Partial<BatchFormData>) => Promise<void>;
  deleteBatch: (id: number) => Promise<void>;
  selectBatch: (batch: Batch | null) => void;
}

const demoBatches: Batch[] = [
  { id: 1, name: 'Morning Batch A', timing: '6:00 AM - 8:00 AM', skillLevel: 'beginner', maxCapacity: 20, monthlyFee: 1500, studentCount: 0, isActive: true },
  { id: 2, name: 'Evening Batch B', timing: '4:00 PM - 6:00 PM', skillLevel: 'intermediate', maxCapacity: 25, monthlyFee: 1800, studentCount: 0, isActive: true },
  { id: 3, name: 'Weekend Batch', timing: '9:00 AM - 12:00 PM', skillLevel: 'advanced', maxCapacity: 15, monthlyFee: 2000, studentCount: 0, isActive: true },
];

export const useBatchStore = create<BatchState>((set, get) => ({
  batches: demoBatches,
  selectedBatch: null,

  fetchBatches: async () => {
    await new Promise(r => setTimeout(r, 300));
  },

  createBatch: async (data) => {
    await new Promise(r => setTimeout(r, 400));
    const newBatch: Batch = {
      id: Math.max(...get().batches.map(b => b.id)) + 1,
      ...data,
      studentCount: 0,
      isActive: true,
    };
    set(state => ({ batches: [...state.batches, newBatch] }));
  },

  updateBatch: async (id, data) => {
    await new Promise(r => setTimeout(r, 300));
    set(state => ({
      batches: state.batches.map(b => b.id === id ? { ...b, ...data } : b),
    }));
  },

  deleteBatch: async (id) => {
    await new Promise(r => setTimeout(r, 300));
    set(state => ({
      batches: state.batches.filter(b => b.id !== id),
    }));
  },

  selectBatch: (batch) => set({ selectedBatch: batch }),
}));
