import { ref } from 'vue'
import { defineStore } from 'pinia'
import type { Company, CompanyParams } from '../types'

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

  companies.value.push(
    {id: 11, project_id: 11, name: 'Рога и копытца', logo: '', description: 'Монтажники вывески' },
    {id: 21, project_id: 12, name: 'ООО "Василек33"', logo: '' },
    {id: 13, project_id: 13, name: 'Откат и деньги', logo: '', description: 'Договариваются с администрацией' },
  )
  
  function companyById (id :number) {
    return companies.value.find(el =>el.id === id)
  }
  
  function addCompany (company: CompanyParams) {
    companies.value.push({
      id: Date.now(),
      project_id: Date.now() * 1000,
      ...company
    })
  }

  function updateCompany (id :number, company: CompanyParams) {
    const idx = companies.value.findIndex(item => item.id === id)
    Object.assign(companies.value[idx] || {}, company)
  }

  function deleteCompany (id :number) {
    const idx = companies.value.findIndex(item => item.id === id)
    companies.value.splice(idx, 1)
  }

  return { companies, addCompany, updateCompany, deleteCompany, companyById }
})
