import { createSlice, createAsyncThunk, type PayloadAction } from "@reduxjs/toolkit"

// Define a type for the contract coverage
export interface ContractCoverage {
  id: number
  name: string
  description: string
}

// Define the state type
interface ContractCoverageState {
  items: ContractCoverage[]
  status: "idle" | "loading" | "succeeded" | "failed"
  error: string | null
  filters: {
    search: string
  }
}

// Initial state
const initialState: ContractCoverageState = {
  items: [],
  status: "idle",
  error: null,
  filters: {
    search: "",
  },
}

// Sample data for initial development
const sampleContractCoverage: ContractCoverage[] = [
  {
    id: 1,
    name: "Airport Pickup & Drop",
    description: "Transportation service between airport and specified location",
  },
  {
    id: 2,
    name: "Hospital Pickup & Drop",
    description: "Transportation service between hospital and specified location",
  },
  {
    id: 3,
    name: "Accommodation Pickup & Drop",
    description: "Transportation service between accommodation and specified location",
  },
  {
    id: 4,
    name: "Point-to-Point Transport",
    description: "Direct transportation between two specified locations",
  },
  {
    id: 5,
    name: "Personal Visits",
    description: "Transportation for personal visits to specified locations",
  },
  {
    id: 6,
    name: "10 Hours a Day",
    description: "Vehicle availability for 10 hours per day with specified mileage limit",
  },
  {
    id: 7,
    name: "24/7 Availability",
    description: "Round-the-clock vehicle availability for specified duration",
  },
  {
    id: 8,
    name: "Corporate Events",
    description: "Transportation services for corporate events and functions",
  },
  {
    id: 9,
    name: "Wedding Transport",
    description: "Transportation services for wedding ceremonies and related events",
  },
  {
    id: 10,
    name: "Tour Package",
    description: "Transportation services for sightseeing and tourism activities",
  },
  {
    id: 11,
    name: "School/College Transport",
    description: "Regular transportation service for educational institutions",
  },
  {
    id: 12,
    name: "Employee Shuttle",
    description: "Regular transportation service for employees between specified locations",
  },
]

// Define the payload type for adding a new contract coverage
export interface AddContractCoveragePayload {
  name: string
  description: string
}

// Async thunks for API calls
export const fetchContractCoverage = createAsyncThunk(
  "contractCoverage/fetchContractCoverage",
  async (_, { rejectWithValue }) => {
    try {
      // In a real app, this would be an API call
      await new Promise((resolve) => setTimeout(resolve, 1000))
      return sampleContractCoverage
    } catch (error) {
      return rejectWithValue("Failed to fetch contract coverage types")
    }
  },
)

export const addContractCoverage = createAsyncThunk(
  "contractCoverage/addContractCoverage",
  async (payload: AddContractCoveragePayload, { rejectWithValue }) => {
    try {
      // In a real app, this would be an API call
      await new Promise((resolve) => setTimeout(resolve, 1000))

      // Generate a new ID (in a real app, the server would do this)
      return {
        id: Date.now(),
        name: payload.name,
        description: payload.description,
      }
    } catch (error) {
      return rejectWithValue("Failed to add contract coverage")
    }
  },
)

export const deleteContractCoverage = createAsyncThunk(
  "contractCoverage/deleteContractCoverage",
  async (id: number, { rejectWithValue }) => {
    try {
      // In a real app, this would be an API call
      await new Promise((resolve) => setTimeout(resolve, 1000))
      return id
    } catch (error) {
      return rejectWithValue("Failed to delete contract coverage")
    }
  },
)

// Create the slice
const contractCoverageSlice = createSlice({
  name: "contractCoverage",
  initialState,
  reducers: {
    updateFilters: (state, action: PayloadAction<Partial<ContractCoverageState["filters"]>>) => {
      state.filters = { ...state.filters, ...action.payload }
    },
    resetFilters: (state) => {
      state.filters = initialState.filters
    },
  },
  extraReducers: (builder) => {
    builder
      .addCase(fetchContractCoverage.pending, (state) => {
        state.status = "loading"
      })
      .addCase(fetchContractCoverage.fulfilled, (state, action) => {
        state.status = "succeeded"
        state.items = action.payload
      })
      .addCase(fetchContractCoverage.rejected, (state, action) => {
        state.status = "failed"
        state.error = action.payload as string
      })
      .addCase(addContractCoverage.fulfilled, (state, action) => {
        state.items.push(action.payload)
      })
      .addCase(deleteContractCoverage.fulfilled, (state, action) => {
        state.items = state.items.filter((item) => item.id !== action.payload)
      })
  },
})

export const { updateFilters, resetFilters } = contractCoverageSlice.actions

export default contractCoverageSlice.reducer

