import { StateCreator } from 'zustand'
import { v4 as uuidv4 } from 'uuid'
import { ServiceBill } from '@/lib/types'

export interface ServicesSlice {
    serviceBills: ServiceBill[]

    addServiceBill: (bill: Omit<ServiceBill, 'id' | 'isPaid'>) => void
    deleteServiceBill: (id: string) => void
    toggleServiceBillPaid: (id: string) => void
}

export const createServicesSlice: StateCreator<ServicesSlice> = (set) => ({
    serviceBills: [],

    addServiceBill: (bill) =>
        set((state) => ({
            serviceBills: [
                ...state.serviceBills,
                { ...bill, id: uuidv4(), isPaid: false },
            ],
        })),

    deleteServiceBill: (id) =>
        set((state) => ({
            serviceBills: state.serviceBills.filter((b) => b.id !== id),
        })),

    toggleServiceBillPaid: (id) =>
        set((state) => ({
            serviceBills: state.serviceBills.map((b) =>
                b.id === id ? { ...b, isPaid: !b.isPaid } : b
            ),
        })),
})
