<script>
import {computed} from 'vue'
import {mapActions, mapGetters} from 'vuex'
import {useStore}               from '@store/index.js'

import AdapterOfServices    from '@adapters/adapterOfServices/Services'
import AdapterOfUsers       from '@adapters/adapterOfUsers/Users'
import ServiceOfUsers       from '@services/serviceOfUsers/Users.js'
import ServiceOfServices    from '@services/serviceOfServices/Services.js'
import CaseOfUsersInService from '@useCases/CaseOfUsersInService.js'

import PageHeader               from "@atoms/AppPageHeader.vue"
import SiteList                 from "@organisms/ServicesList/ServicesList.vue"
import SiteManager              from "@organisms/UsersManager/UsersManager.vue"

export default {
  name: 'SitesManagerPage',
  components: {SiteManager, SiteList, PageHeader},
  provide() {
    return {
      serviceOfServices: computed(() => this.serviceOfServices),
    }
  },
  setup() {
    const url = import.meta.env.VITE_API_ADDR

    const adapterOfServices = new AdapterOfServices(url)
    const adapterOfUsers    = new AdapterOfUsers(url)

    const store = useStore()

    const serviceOfUsers =    new ServiceOfUsers(adapterOfUsers, store)
    const serviceOfServices = new ServiceOfServices(adapterOfServices, store)
    // serviceOfServices.fetchServicesList()

    const caseOfUsersInService = new CaseOfUsersInService(serviceOfUsers, serviceOfServices)

    return {serviceOfUsers, serviceOfServices, caseOfUsersInService}
  },
  data() {
    return {
    }
  },
  computed: {
    ...mapGetters('services', ["sites", "routes", "newRoute", "selectedServiceState"]),
  },
  mounted () {
    // this.uploadServices()
  },
  methods: {
    ...mapActions('services', ["uploadServices", "uploadAndSelectService"]),
    ...mapActions('users', ["fetchUsersList"]),
    async selectSite(siteId, mode = 'prod') {
      await this.fetchUsersList({siteId, mode})
      this.uploadAndSelectService(siteId)
      return "ok"
    }
  }
}
</script>

<template>
  <div class="p-6">
    <PageHeader class="me-2 mb-6" />
    <SiteList
      :serviceOfServices="serviceOfServices"
      :caseOfUsersInService="caseOfUsersInService"
      :selectSite="selectSite"
    />
    <SiteManager
      v-if="selectedServiceState === 'active'"
      :serviceOfUsers="serviceOfUsers"
      :caseOfUsersInService="caseOfUsersInService"
    />
  </div>
</template>
