import { computed, ref } from 'vue'
import { defineStore } from 'pinia'
import http from '../api/http'
import {
  authStorageKeys,
  clearAuthSession,
  isNativeApp,
  saveAuthSession,
} from '../auth/session'

export interface CurrentUser {
  id: string
  userName: string
  displayName: string
  roles: string[]
  collegeId?: string
  effectiveDataScope: 'Self' | 'Class' | 'College' | 'All'
}

export const useAuthStore = defineStore('auth', () => {
  const token = ref(localStorage.getItem(authStorageKeys.token) ?? '')
  const saved = localStorage.getItem(authStorageKeys.user)
  const user = ref<CurrentUser | null>(saved ? JSON.parse(saved) : null)
  const isLoggedIn = computed(() => Boolean(token.value))
  const isSuperAdmin = computed(() => user.value?.roles.includes('SuperAdmin') ?? false)

  async function login(userName: string, password: string) {
    const { data } = await http.post('/auth/login', {
      userName,
      password,
      isNativeApp: isNativeApp(),
    })
    token.value = data.token
    user.value = data.user
    saveAuthSession(data)
  }

  async function exchangeSso(code: string) {
    const { data } = await http.post('/auth/sso/exchange', {
      code,
      isNativeApp: isNativeApp(),
    })
    token.value = data.token
    user.value = data.user
    saveAuthSession(data)
  }

  async function bindSso(code: string, userName: string, password: string) {
    const { data } = await http.post('/auth/sso/bind', {
      code,
      userName,
      password,
      isNativeApp: isNativeApp(),
    })
    token.value = data.token
    user.value = data.user
    saveAuthSession(data)
  }

  async function refresh() {
    if (!token.value) return
    const { data } = await http.get('/auth/me')
    user.value = data
    localStorage.setItem(authStorageKeys.user, JSON.stringify(data))
  }

  function logout() {
    const refreshToken = localStorage.getItem(authStorageKeys.refreshToken)
    if (refreshToken) void http.post('/auth/logout', { refreshToken }).catch(() => undefined)
    token.value = ''
    user.value = null
    clearAuthSession()
  }

  window.addEventListener('mingxu-auth-changed', () => {
    token.value = localStorage.getItem(authStorageKeys.token) ?? ''
    const currentUser = localStorage.getItem(authStorageKeys.user)
    user.value = currentUser ? JSON.parse(currentUser) : null
  })

  return {
    token,
    user,
    isLoggedIn,
    isSuperAdmin,
    login,
    exchangeSso,
    bindSso,
    refresh,
    logout,
  }
})
