import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { api } from 'boot/axios'
import type { Company, CompanyParams, CompanyMask } from 'types/Company'
import { useProjectsStore } from 'stores/projects'

export const useCompaniesStore = defineStore('companies', () => {
  const projectsStore = useProjectsStore()
  const companies = ref<Company[]>([])
  const companiesMask = ref<CompanyMask[]>([])

  const isInit = ref<boolean>(false)

  const currentProjectId = computed(() => projectsStore.currentProjectId)
  
  async function init () {
    const { data }= await api.get('/project/' + currentProjectId.value + '/company')
    const companiesAPI = data.data
    companies.value.push(...(companiesAPI.sort((a: Company, b: Company) => (a.id - b.id))))
    companiesMask.value = [ 
      { "company_id": 11, "company_list": [ 9, 12 ] },
      { "company_id": 9, "company_list": [ 11, 12 ] },
      { "company_id": 12, "company_list": [ 9, 10, 13 ] }
    ]
    // await getCompanyMasked()
    isInit.value = true
  }

  function reset () {
    companies.value = []
    companiesMask.value = []
    isInit.value = false
  }
  
  async function add (companyData: CompanyParams) {
    const { data } = await api.post('/project/' + currentProjectId.value + '/company', companyData)
    const newCompanyAPI = data.data
    companies.value.push(newCompanyAPI)
    return newCompanyAPI
  }

  async function update (companyId: number, companyData: CompanyParams) {
    const { data } = await api.put('/project/' + currentProjectId.value + '/company/' + companyId, companyData)
    const companyAPI = data.data
    const idx = companies.value.findIndex(item => item.id === companyAPI.id)
    if (companies.value[idx]) Object.assign(companies.value[idx], companyAPI)
  }

  async function remove (companyId: number) {
    const { data } = await api.delete('/project/' + currentProjectId.value + '/company/' + companyId)
    const companyAPIid = data.data.id
    const idx = companies.value.findIndex(item => item.id === companyAPIid)
    companies.value.splice(idx, 1)
    await getCompanyMasked()
  }

  function companyById (id: number) {
    return companies.value.find(el =>el.id === id)
  }

  async function getCompanyMasked () {
    const { data } = await api.get('/project/' + currentProjectId.value + '/company/mapping')
    const companiesMaskAPI = data.data
    companiesMask.value = companiesMaskAPI
  }

  function checkCompanyMasked (id: number) {
    return companiesMask.value.some(el => el.company_id === id)
  }

  async function updateMask (mask: CompanyMask[]) {
    const { data } = await api.post('/project/' + currentProjectId.value + '/company/mapping', mask)
    const maskAPI = data.data
    companiesMask.value = maskAPI
  }

  const getCompanies = computed(() => companies.value)

  return {
    companies,
    companiesMask,
    isInit,
    init,
    reset,
    add,
    update,
    remove,
    companyById,
    checkCompanyMasked,
    updateMask,
    getCompanies
  }
})

