import { ref, computed } from 'vue'

export interface Service {
  id: number
  name: string
  lat: number
  lng: number
  status: 'pending' | 'approved'
  address: string
  distance: number
  category: string
}

export interface Scope {
  id: string
  label: string
  bounds: [[number, number], [number, number]] // SW, NE corners
}

export const useServiceMap = () => {
  // Mock services around Budapest
  const services = ref<Service[]>([
    {
      id: 1,
      name: 'AutoService Budapest',
      lat: 47.6333,
      lng: 19.1333,
      status: 'pending',
      address: 'Budapest, Kossuth Lajos utca 12',
      distance: 0.5,
      category: 'Car Repair'
    },
    {
      id: 2,
      name: 'MOL Station',
      lat: 47.6400,
      lng: 19.1400,
      status: 'approved',
      address: 'Budapest, Váci út 45',
      distance: 1.2,
      category: 'Fuel Station'
    },
    {
      id: 3,
      name: 'TireMaster',
      lat: 47.6200,
      lng: 19.1200,
      status: 'pending',
      address: 'Budapest, Üllői út 78',
      distance: 2.1,
      category: 'Tire Service'
    },
    {
      id: 4,
      name: 'CarWash Express',
      lat: 47.6500,
      lng: 19.1500,
      status: 'approved',
      address: 'Budapest, Róna utca 5',
      distance: 3.0,
      category: 'Car Wash'
    },
    {
      id: 5,
      name: 'BrakeCenter',
      lat: 47.6100,
      lng: 19.1100,
      status: 'pending',
      address: 'Budapest, Könyves Kálmán körút 32',
      distance: 2.5,
      category: 'Brake Service'
    },
    {
      id: 6,
      name: 'ElectricCar Service',
      lat: 47.6000,
      lng: 19.1000,
      status: 'pending',
      address: 'Budapest, Hungária körút 120',
      distance: 4.2,
      category: 'EV Charging'
    },
    {
      id: 7,
      name: 'OilChange Pro',
      lat: 47.6700,
      lng: 19.1700,
      status: 'approved',
      address: 'Budapest, Szentmihályi út 67',
      distance: 5.1,
      category: 'Oil Change'
    },
    {
      id: 8,
      name: 'BodyShop Elite',
      lat: 47.5900,
      lng: 19.0900,
      status: 'pending',
      address: 'Budapest, Gyáli út 44',
      distance: 5.8,
      category: 'Body Repair'
    }
  ])

  // Simulated RBAC geographical scope
  const currentScope = ref<Scope>({
    id: 'pest_county',
    label: 'Pest County / Central Hungary',
    bounds: [[47.3, 18.9], [47.8, 19.5]]
  })

  const scopeLabel = computed(() => currentScope.value.label)

  const pendingServices = computed(() => 
    services.value.filter(s => s.status === 'pending')
  )

  const approvedServices = computed(() => 
    services.value.filter(s => s.status === 'approved')
  )

  const approveService = (serviceId: number) => {
    const service = services.value.find(s => s.id === serviceId)
    if (service) {
      service.status = 'approved'
      console.log(`Service ${serviceId} approved`)
    }
  }

  const addMockService = (service: Omit<Service, 'id'>) => {
    const newId = Math.max(...services.value.map(s => s.id)) + 1
    services.value.push({
      id: newId,
      ...service
    })
  }

  const filterByScope = (servicesList: Service[]) => {
    const [sw, ne] = currentScope.value.bounds
    return servicesList.filter(s => 
      s.lat >= sw[0] && s.lat <= ne[0] && 
      s.lng >= sw[1] && s.lng <= ne[1]
    )
  }

  const servicesInScope = computed(() => 
    filterByScope(services.value)
  )

  const changeScope = (scope: Scope) => {
    currentScope.value = scope
  }

  // Available scopes for simulation
  const availableScopes: Scope[] = [
    {
      id: 'budapest',
      label: 'Budapest Only',
      bounds: [[47.4, 19.0], [47.6, 19.3]]
    },
    {
      id: 'pest_county',
      label: 'Pest County / Central Hungary',
      bounds: [[47.3, 18.9], [47.8, 19.5]]
    },
    {
      id: 'hungary',
      label: 'Whole Hungary',
      bounds: [[45.7, 16.1], [48.6, 22.9]]
    }
  ]

  return {
    services,
    pendingServices,
    approvedServices,
    scopeLabel,
    currentScope,
    servicesInScope,
    approveService,
    addMockService,
    changeScope,
    availableScopes
  }
}