import { defineStore } from 'pinia'
import { ref, reactive, computed } from 'vue'
import { useRoute } from 'vue-router'
import { useCustomerStore } from '@/module/uyeler/stores/customerStore'
import { useAppUserStore } from '@/module/kullanicilar/stores/appUserStore'

export const useUsersStore = defineStore('usersStore', () => {
  const route = useRoute()
  const customerStore = useCustomerStore()
  const appUserStore = useAppUserStore()

  const userStorageKeys = reactive<Record<string, any>>({
    USER: 'username',
    TOKEN: 'token',
    IS_AUTH: 'is_auth',
    ROLES: 'roles',
    ID: 'id',
    STATUS: 'status',
    POSITION: 'position',
    DEPARTMENT: 'department'
  })

  const userId = ref<number>(0)
  const userApproveId = ref<number | null>(null)
  const userSubApproveId = ref<number | null>(null)
  const userName = ref<string>('')
  const userMail = ref<string>('')
  const userIsAuth = ref<boolean>(false)
  const userRoles = ref<string[]>([])
  const userStatus = ref<number | null>(null)
  const userPosition = ref<number | null>(null)
  const userDepartment = ref<number | null>(null)
  const profilePhoto = ref<string>('')

  const SetSelectedUser = () => {
    if (route.name === 'UyeYetkiliDetay') {
      customerStore.selectedCustomer = Number(route.params.uyeId)
      customerStore.selectedSubCustomer = Number(route.params.altUyeId)
    } else if (route.name === 'UyeDetay')
      customerStore.selectedCustomer = Number(route.params.uyeId)
    else if (route.name === 'Profil') {
      if (isAdmin.value) appUserStore.selectedAppUser = userId.value
      else customerStore.selectedCustomer = userId.value
    }
  }

  const selectedUserId = ():number => {
    var id: number = Number(userId.value)
    if (route.name === 'UyeYetkiliDetay') {
      id = Number(route.params.altUyeId)
    } else if (route.name === 'UyeDetay') {
      id = Number(route.params.uyeId)
    } else if (route.name === 'Profil') {
      id = Number(userId.value)
    }
    return id
  }

  const selectedUserApproveId = computed<number | null>(() => {
    var id: number | null = null
    if (route.name === 'UyeYetkiliDetay') {
      id = userSubApproveId.value
    } else if (route.name === 'UyeDetay' || route.name === 'Profil') {
      id = userApproveId.value
    }

    return id
  })

  const SetUserSessionData = (data: Record<string, any>) => {
    sessionStorage.setItem(userStorageKeys.TOKEN, data.token)
    sessionStorage.setItem(userStorageKeys.USER, data.username)
    sessionStorage.setItem(userStorageKeys.IS_AUTH, JSON.stringify(true))
    sessionStorage.setItem(userStorageKeys.ROLES, JSON.stringify(data.roles))
    sessionStorage.setItem(userStorageKeys.ID, JSON.stringify(data.id))
    sessionStorage.setItem(userStorageKeys.STATUS, JSON.stringify(data.islemtipi))
    sessionStorage.setItem(userStorageKeys.POSITION, JSON.stringify(data.gorevId))
    sessionStorage.setItem(userStorageKeys.DEPARTMENT, JSON.stringify(data.birimId))
  }

  const RemoveUserSessionData = () => {
    sessionStorage.removeItem(userStorageKeys.TOKEN)
    sessionStorage.removeItem(userStorageKeys.USER)
    sessionStorage.removeItem(userStorageKeys.IS_AUTH)
    sessionStorage.removeItem(userStorageKeys.ROLES)
    sessionStorage.removeItem(userStorageKeys.ID)
    sessionStorage.removeItem(userStorageKeys.STATUS)
    sessionStorage.removeItem(userStorageKeys.POSITION)
    sessionStorage.removeItem(userStorageKeys.DEPARTMENT)
  }

  const SetUserData = () => {
    let token = sessionStorage.getItem(userStorageKeys.TOKEN)
    if (token) {
      userId.value = Number(sessionStorage.getItem(userStorageKeys.ID)) as number

      userName.value = sessionStorage.getItem(userStorageKeys.USER) as string

      userIsAuth.value =
        sessionStorage.getItem(userStorageKeys.IS_AUTH) === 'true' || true ? true : false

      userRoles.value = JSON.parse(
        sessionStorage.getItem(userStorageKeys.ROLES) as string
      ) as string[]

      userStatus.value = Number(sessionStorage.getItem(userStorageKeys.STATUS))

      if (sessionStorage.getItem(userStorageKeys.POSITION) !== undefined)
        userPosition.value = Number(sessionStorage.getItem(userStorageKeys.POSITION))

      if (sessionStorage.getItem(userStorageKeys.DEPARTMENT) !== undefined)
        userDepartment.value = Number(sessionStorage.getItem(userStorageKeys.DEPARTMENT))

      if (isPanelUser.value) {
        userMail.value = sessionStorage.getItem(userStorageKeys.USER) as string
      }
    }
  }

  const isAdmin = computed<boolean>(() => {
    return userRoles.value.includes('admin')
  })

  const isSuperAdmin = computed<boolean>(() => {
    return userRoles.value.includes('superadmin')
  })

  const isAccounting = computed<boolean>(() => {
    return userDepartment.value === 6 || userPosition.value === 5
  })

  const isPanelUser = computed<boolean>(() => {
    return isAdmin.value || isSuperAdmin.value
  })

  const isAraciFirma = computed<boolean>(() => {
    return userRoles.value.includes('AraciFirma')
  })

  const isGercekKisi = computed<boolean>(() => {
    return userRoles.value.includes('gercekkisi')
  })

  const isTuzelKisi = computed<boolean>(() => {
    return userRoles.value.includes('tuzelkisi')
  })

  const isVakifDernek = computed<boolean>(() => {
    return userRoles.value.includes('vakifdernek')
  })

  const ResetUserData = () => {
    RemoveUserSessionData()
    userId.value = 0
    userApproveId.value = null
    userSubApproveId.value = null
    userName.value = ''
    userMail.value = ''
    userIsAuth.value = false
    userRoles.value = []
    userStatus.value = null
    profilePhoto.value = ''
  }

  return {
    userStorageKeys,
    userId,
    userName,
    userMail,
    userIsAuth,
    userRoles,
    userStatus,
    userPosition,
    userApproveId,
    userSubApproveId,
    selectedUserApproveId,
    profilePhoto,
    selectedUserId,
    isAdmin,
    isSuperAdmin,
    isPanelUser,
    isAccounting,
    isAraciFirma,
    isGercekKisi,
    isTuzelKisi,
    isVakifDernek,
    SetSelectedUser,
    SetUserSessionData,
    RemoveUserSessionData,
    SetUserData,
    ResetUserData
  }
})
