import { BaseOrganizationProfile } from '~/types/interfaces'
import { defineStore } from "pinia";
import {computed, Ref} from "@vue/reactivity";

export const useOrganizationProfileStore = defineStore('organizationProfile', () => {

  // State
  const id: Ref<number | null> = ref(null)
  const parametersId: Ref<number | null> = ref(null)
  const name: Ref<string | null> = ref(null)
  const product: Ref<string | null> = ref(null)
  const currentActivityYear: Ref<number | null> = ref(null)
  const modules:  Ref<Array<string>> = ref([])
  const hasChildren: Ref<boolean | null> = ref(false)
  const legalStatus: Ref<string | null> = ref(null)
  const showAdherentList: Ref<boolean | null> = ref(false)
  const networks:  Ref<Array<string>> = ref([])
  const website: Ref<string | null>  = ref(null)
  const parents: Ref<Array<BaseOrganizationProfile>> = ref([])

  const runtimeConfig = useRuntimeConfig()

  // Getters
  /**
   * L'organization fait-elle partie du réseau CMF ?
   *
   * @return {boolean}
   */
  const isCmf = computed( (): boolean => {
    return networks.value.filter((network: string) => {
      return network === runtimeConfig.cmf_network
    }).length > 0
  })

  /**
   * L'organization fait-elle partie du réseau FFEC ?
   *
   * @return {boolean}
   */
  const isFfec = computed( (): boolean => {
    return networks.value.filter((network: string) => {
      return network === runtimeConfig.ffec_network
    }).length > 0
  })

  /**
   * L'organization fait-elle partie d'un réseau ?
   *
   * @return {boolean}
   */
  const isInsideNetwork = computed( (): boolean => {
    return isCmf.value || isFfec.value
  })

  /**
   * L'organization possède t'elle un produit premium
   * @return {boolean}
   */
  const isArtistProduct = computed( (): boolean => {
    return product.value === runtimeConfig.artist_product
  })

  /**
   * L'organization possède t'elle un produit artiste premium
   * @return {boolean}
   */
  const isArtistPremiumProduct = computed( (): boolean => {
    return product.value === runtimeConfig.artist_premium_product
  })

  /**
   * L'organization possède-t-elle un produit artiste ou artiste premium
   * @return {boolean}
   */
  const isArtist = computed( (): boolean => {
    return isArtistProduct.value || isArtistPremiumProduct.value
  })

  /**
   * L'organization possède-t-elle un produit school?
   *
   * @return {boolean}
   */
  const isSchoolProduct = computed( (): boolean => {
    return product.value === runtimeConfig.school_product
  })

  /**
   * L'organization possède-t-elle un produit school-premium?
   *
   * @return {boolean}
   */
  const isSchoolPremiumProduct = computed( (): boolean => {
    return product.value === runtimeConfig.school_premium_product
  })

  /**
   * L'organization possède-t-elle un produit school ou school-premium
   * @return {boolean}
   */
  const isSchool = computed( (): boolean => {
    return isSchoolProduct.value || isSchoolPremiumProduct.value
  })

  /**
   * L'organization possède t'elle un produit manager
   * @return {boolean}
   */
  const isManagerProduct = computed( (): boolean => {
    return product.value === runtimeConfig.manager_product
  })

  /**
   * L'organization peut-elle afficher la lister des adhérents avec leurs coordonnées
   * @return {boolean|null}
   */
  const isShowAdherentList = computed(():any => {
    return showAdherentList.value;
  })

  /**
   * L'organization est elle une association ?
   * @return {boolean|null}
   */
  const isAssociation = computed(():any => {
    return legalStatus.value === 'ASSOCIATION_LAW_1901';
  })

  /**
   * L'organization est-elle la CMF ?
   *
   * @return {boolean}
   */
  const isCMFCentralService = computed((): boolean => {
    if(runtimeConfig.CMF_ID)
      return id.value === parseInt(runtimeConfig.CMF_ID)
    return false
  })

  const getWebsite = computed((): string | null => {
    return website.value ?? null
  })

  const hasModule = (module: string): boolean => {
    return modules.value.includes(module)
  }

  // Actions
  const setProfile = (profile: any) => {
    id.value = profile.id
    parametersId.value = profile.parametersId
    name.value = profile.name
    product.value = profile.product
    currentActivityYear.value = profile.currentYear
    website.value = profile.website
    modules.value = profile.modules
    hasChildren.value = profile.hasChildren
    legalStatus.value = profile.legalStatus
    showAdherentList.value = profile.showAdherentList
    networks.value = profile.networks

    useEach(profile.parents, (parent) => {
      parents.value.push({
        id: parent.id,
        name: parent.name,
        website: parent.website
      })
    })
  }

  const refreshProfile = (profile: any) => {
    name.value = profile.name
    currentActivityYear.value = profile.currentYear
    website.value = profile.website
    legalStatus.value = profile.legalStatus
  }

  return {
    id,
    parametersId,
    name,
    product,
    currentActivityYear,
    modules,
    hasChildren,
    legalStatus,
    showAdherentList,
    networks,
    website,
    parents,
    isCmf,
    isFfec,
    isInsideNetwork,
    isArtistProduct,
    isArtistPremiumProduct,
    isArtist,
    isSchoolPremiumProduct,
    isSchoolProduct,
    isSchool,
    isManagerProduct,
    isShowAdherentList,
    isAssociation,
    getWebsite,
    hasModule,
    setProfile,
    refreshProfile
  }
})
