import { defineStore } from 'pinia' import { consultationApi, chartApi, annotationApi } from '../utils/api' export const useChartStore = defineStore('chart', { state: () => ({ currentName: '', currentBirthYear: 0, currentBirthMonth: 0, currentBirthDay: 0, currentChart: null, currentRecordId: null, currentQuestions: '', records: [], annotations: [], tags: [], consultationMessages: [] }), actions: { /** * Start consultation via backend API (US-1.1 §13 — main path only). * All computation is on the backend CalculatorService. */ async startConsultation(name, birthday, questions) { const result = await consultationApi.start({ name, birthday, questions }) const record = result.record this.currentRecordId = record.id this.currentName = record.name || name this.currentQuestions = questions || '' this.consultationMessages = result.messages || [] // Parse backend chart data if (record.chartData) { const parsed = typeof record.chartData === 'string' ? JSON.parse(record.chartData) : record.chartData this.currentChart = parsed.positions || parsed } return result }, async saveChart(name, birthday, questions) { const chartData = JSON.stringify(this.currentChart) const record = await chartApi.create({ name, birthday, chartData, questions }) this.currentRecordId = record.id || null this.currentQuestions = questions || '' return record }, async loadChart(id) { const record = await chartApi.detail(id) if (record && record.chartData) { this.currentChart = typeof record.chartData === 'string' ? JSON.parse(record.chartData) : record.chartData this.currentRecordId = id } return record }, async fetchRecords() { this.records = await chartApi.list() }, async fetchAnnotations(recordId) { this.annotations = await annotationApi.list(recordId) }, async addAnnotation(recordId, position, content) { return await annotationApi.add({ recordId, position, content }) }, async fetchTags(recordId) { this.tags = await annotationApi.listTags(recordId) }, async addTag(recordId, tag) { return await annotationApi.addTag({ recordId, tag }) } } })