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

// Define types
interface Address {
  line1: string
  line2: string
  country: string
  state: string
  city: string
  zipcode: string
}

export interface ClientCompany {
  id: number
  name: string
  clientTypeId: number
  clientTypeName: string
  address: Address
  countryCode: string
  phoneNumber: string
  email: string
  website: string
  allowPlatformAccess: boolean
  contactsCount: number
  hasContract: boolean
}

interface ClientCompaniesState {
  items: ClientCompany[]
  status: "idle" | "loading" | "succeeded" | "failed"
  error: string | null
}

// Initial state
const initialState: ClientCompaniesState = {
  items: [
    {
      id: 1,
      name: "Acme Corporation",
      clientTypeId: 3,
      clientTypeName: "Corporate",
      address: {
        line1: "123 Business Ave",
        line2: "Suite 200",
        country: "United States",
        state: "California",
        city: "San Francisco",
        zipcode: "94107",
      },
      countryCode: "+1",
      phoneNumber: "555-123-4567",
      email: "info@acmecorp.com",
      website: "www.acmecorp.com",
      allowPlatformAccess: true,
      contactsCount: 3,
      hasContract: true,
    },
    {
      id: 2,
      name: "Globex Industries",
      clientTypeId: 3,
      clientTypeName: "Corporate",
      address: {
        line1: "456 Enterprise Blvd",
        line2: "Floor 12",
        country: "United Kingdom",
        state: "England",
        city: "London",
        zipcode: "EC1A 1BB",
      },
      countryCode: "+44",
      phoneNumber: "20-7946-0958",
      email: "contact@globex.co.uk",
      website: "www.globex.co.uk",
      allowPlatformAccess: true,
      contactsCount: 2,
      hasContract: true,
    },
    {
      id: 3,
      name: "Stark Enterprises",
      clientTypeId: 3,
      clientTypeName: "Corporate",
      address: {
        line1: "789 Innovation Way",
        line2: "",
        country: "United States",
        state: "New York",
        city: "New York",
        zipcode: "10001",
      },
      countryCode: "+1",
      phoneNumber: "212-555-7890",
      email: "hello@starkent.com",
      website: "www.starkenterprises.com",
      allowPlatformAccess: true,
      contactsCount: 1,
      hasContract: false,
    },
    {
      id: 4,
      name: "Wayne Industries",
      clientTypeId: 3,
      clientTypeName: "Corporate",
      address: {
        line1: "1007 Mountain Drive",
        line2: "",
        country: "United States",
        state: "Illinois",
        city: "Chicago",
        zipcode: "60601",
      },
      countryCode: "+1",
      phoneNumber: "312-555-0123",
      email: "info@wayneindustries.com",
      website: "www.wayneindustries.com",
      allowPlatformAccess: false,
      contactsCount: 2,
      hasContract: true,
    },
    {
      id: 5,
      name: "City General Hospital",
      clientTypeId: 2,
      clientTypeName: "Hospital",
      address: {
        line1: "789 Medical Center Drive",
        line2: "Healthcare Complex",
        country: "United States",
        state: "California",
        city: "Los Angeles",
        zipcode: "90012",
      },
      countryCode: "+1",
      phoneNumber: "213-555-9000",
      email: "admin@citygeneralhospital.com",
      website: "www.citygeneralhospital.com",
      allowPlatformAccess: true,
      contactsCount: 4,
      hasContract: true,
    },
    {
      id: 6,
      name: "Department of Health",
      clientTypeId: 1,
      clientTypeName: "Government",
      address: {
        line1: "1600 Health Services Blvd",
        line2: "Government Building",
        country: "United States",
        state: "Washington",
        city: "Washington D.C.",
        zipcode: "20500",
      },
      countryCode: "+1",
      phoneNumber: "202-555-1234",
      email: "contact@healthdept.gov",
      website: "www.healthdept.gov",
      allowPlatformAccess: true,
      contactsCount: 1,
      hasContract: true,
    },
    {
      id: 7,
      name: "Red Cross Foundation",
      clientTypeId: 5,
      clientTypeName: "Non-Profit",
      address: {
        line1: "544 Charity Ave",
        line2: "Foundation Building",
        country: "United States",
        state: "New York",
        city: "New York",
        zipcode: "10022",
      },
      countryCode: "+1",
      phoneNumber: "212-555-4321",
      email: "info@redcrossfoundation.org",
      website: "www.redcrossfoundation.org",
      allowPlatformAccess: false,
      contactsCount: 0,
      hasContract: false,
    },
    {
      id: 8,
      name: "University Medical Center",
      clientTypeId: 4,
      clientTypeName: "Educational",
      address: {
        line1: "1 University Plaza",
        line2: "Medical Campus",
        country: "United Kingdom",
        state: "England",
        city: "London",
        zipcode: "EC2N 4AY",
      },
      countryCode: "+44",
      phoneNumber: "20-7123-4567",
      email: "contact@umc.ac.uk",
      website: "www.umc.ac.uk",
      allowPlatformAccess: true,
      contactsCount: 2,
      hasContract: true,
    },
    {
      id: 9,
      name: "Elite Travel Services",
      clientTypeId: 7,
      clientTypeName: "Travel Agency",
      address: {
        line1: "33 Travel Plaza",
        line2: "Tourism Building",
        country: "United States",
        state: "Florida",
        city: "Miami",
        zipcode: "33101",
      },
      countryCode: "+1",
      phoneNumber: "305-555-7890",
      email: "bookings@elitetravel.com",
      website: "www.elitetravel.com",
      allowPlatformAccess: true,
      contactsCount: 3,
      hasContract: false,
    },
    {
      id: 10,
      name: "Premier Events Co",
      clientTypeId: 8,
      clientTypeName: "Event Management",
      address: {
        line1: "655 Park Avenue",
        line2: "Floor 108",
        country: "United States",
        state: "New York",
        city: "New York",
        zipcode: "10065",
      },
      countryCode: "+1",
      phoneNumber: "212-555-0147",
      email: "info@massivedynamic.com",
      website: "www.massivedynamic.com",
      allowPlatformAccess: true,
      contactsCount: 3,
      hasContract: true,
    },
  ],
  status: "idle",
  error: null,
}

// Async thunks
export const fetchClientCompanies = createAsyncThunk("clientCompanies/fetchClientCompanies", async () => {
  // Simulate API call
  return new Promise<ClientCompany[]>((resolve) => {
    setTimeout(() => {
      resolve(initialState.items)
    }, 500)
  })
})

export const addClientCompany = createAsyncThunk(
  "clientCompanies/addClientCompany",
  async (company: Omit<ClientCompany, "id" | "contactsCount" | "hasContract" | "clientTypeName">, { getState }) => {
    // Simulate API call
    return new Promise<ClientCompany>((resolve) => {
      setTimeout(() => {
        // Get client types from state to lookup the name
        const state = getState() as any
        const clientTypes = state.clientTypes.items
        const clientType = clientTypes.find((type: any) => type.id === company.clientTypeId)
        
        const newCompany: ClientCompany = {
          ...company,
          id: Date.now(),
          clientTypeName: clientType?.name || "Unknown Type",
          contactsCount: 0,
          hasContract: false,
        }
        resolve(newCompany)
      }, 500)
    })
  },
)

// Create slice
const clientCompaniesSlice = createSlice({
  name: "clientCompanies",
  initialState,
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(fetchClientCompanies.pending, (state) => {
        state.status = "loading"
      })
      .addCase(fetchClientCompanies.fulfilled, (state, action) => {
        state.status = "succeeded"
        state.items = action.payload
      })
      .addCase(fetchClientCompanies.rejected, (state, action) => {
        state.status = "failed"
        state.error = action.error.message || null
      })
      .addCase(addClientCompany.fulfilled, (state, action) => {
        state.items.push(action.payload)
      })
  },
})

export default clientCompaniesSlice.reducer

