import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useApi } from '../composables/useApi'
import type { CoachingChat, ChatListItem, TrainingPlan, ComplianceWeek, TodayWorkout } from '../types/models'

export const useCoachingStore = defineStore('coaching', () => {
  const { api } = useApi()
  const currentChat = ref<CoachingChat | null>(null)
  const activePlan = ref<TrainingPlan | null>(null)
  const todayWorkout = ref<TodayWorkout | null>(null)
  const loading = ref(false)
  const sending = ref(false)

  async function startOnboarding(): Promise<CoachingChat> {
    loading.value = true
    try {
      const { data } = await api.post<CoachingChat>('/coaching/onboarding/start')
      currentChat.value = data
      return data
    } finally {
      loading.value = false
    }
  }

  async function getOnboardingStatus(): Promise<{ onboarding_completed: boolean; coaching_profile: Record<string, unknown> | null }> {
    const { data } = await api.get('/coaching/onboarding/status')
    return data
  }

  async function sendMessage(chatId: string, message: string): Promise<CoachingChat> {
    sending.value = true
    try {
      const { data } = await api.post<CoachingChat & { response: string }>(`/coaching/chat/${chatId}/message`, { message })
      currentChat.value = data
      return data
    } finally {
      sending.value = false
    }
  }

  async function createChat(): Promise<CoachingChat> {
    const { data } = await api.post<CoachingChat>('/coaching/chat/new')
    currentChat.value = data
    return data
  }

  async function getChat(chatId: string): Promise<CoachingChat> {
    const { data } = await api.get<CoachingChat>(`/coaching/chat/${chatId}`)
    currentChat.value = data
    return data
  }

  async function listChats(): Promise<ChatListItem[]> {
    const { data } = await api.get<ChatListItem[]>('/coaching/chats')
    return data
  }

  async function generatePlan(): Promise<TrainingPlan> {
    loading.value = true
    try {
      const { data } = await api.post<TrainingPlan>('/coaching/plan/generate')
      activePlan.value = data
      return data
    } finally {
      loading.value = false
    }
  }

  async function fetchActivePlan(): Promise<TrainingPlan | null> {
    const { data } = await api.get<TrainingPlan | null>('/coaching/plan/active')
    activePlan.value = data
    return data
  }

  async function fetchCompliance(planId: string): Promise<ComplianceWeek[]> {
    const { data } = await api.get<ComplianceWeek[]>(`/coaching/plan/${planId}/compliance`)
    return data
  }

  async function fetchTodayWorkout(): Promise<TodayWorkout | null> {
    const { data } = await api.get<TodayWorkout | null>('/coaching/today')
    todayWorkout.value = data
    return data
  }

  async function startPlanAdjustment(): Promise<CoachingChat & { response: string }> {
    loading.value = true
    try {
      const { data } = await api.post<CoachingChat & { response: string }>('/coaching/plan/adjust')
      currentChat.value = data
      return data
    } finally {
      loading.value = false
    }
  }

  async function linkActivity(activityId: string, planId: string, week: number, day: string): Promise<void> {
    await api.post('/coaching/link', { activity_id: activityId, plan_id: planId, week, day })
  }

  async function unlinkActivity(activityId: string): Promise<void> {
    await api.post(`/coaching/unlink/${activityId}`)
  }

  return {
    currentChat, activePlan, todayWorkout, loading, sending,
    startOnboarding, getOnboardingStatus, sendMessage, createChat, getChat, listChats,
    generatePlan, fetchActivePlan, fetchCompliance, fetchTodayWorkout, startPlanAdjustment,
    linkActivity, unlinkActivity,
  }
})
