/*
|--------------------------------------------------------------------------
| Agência PSI
|--------------------------------------------------------------------------
| Arquivo: src/features/patients/prontuario/services/clinicalNotesRepository.js
|
| Repository de clinical_notes (anamnese, evolução, plano, observação livre,
| resumo de caso). RLS owner-only (CFP sigilo profissional).
|
| Schema: ver migrations/20260520000001_clinical_notes_tables.sql
| Trigger AUTO-versiona em INSERT e UPDATE de content/title/deleted_at.
|
| ⚠️ Pré-requisito: migrations 20260520000001..3 executadas no banco.
|    Sem isso, todas as funções abaixo retornam erro de "tabela não existe".
|--------------------------------------------------------------------------
*/
import { supabase } from '@/lib/supabase/client';
import { tenantDb } from '@/lib/supabase/tenantClient';
import { useTenantStore } from '@/stores/tenantStore';
import { assertTenantId, getUid } from './_tenantGuards';
import {
    CLINICAL_NOTE_SELECT,
    CLINICAL_NOTE_SELECT_BRIEF,
    CLINICAL_NOTE_VERSION_SELECT,
    flattenNoteRow
} from './clinicalNotesSelects';

const VALID_NOTE_TYPES = ['anamnese', 'evolucao_sessao', 'plano_terapeutico', 'observacao_livre', 'resumo_caso'];

function resolveTenantId(tenantIdArg) {
    const tenantStore = useTenantStore();
    const tenantId = tenantIdArg || tenantStore.activeTenantId || tenantStore.tenantId;
    assertTenantId(tenantId);
    return tenantId;
}

function assertNoteType(noteType) {
    if (!VALID_NOTE_TYPES.includes(noteType)) {
        throw new Error(`Tipo de nota inválido. Aceitos: ${VALID_NOTE_TYPES.join(', ')}.`);
    }
}

/**
 * Lista notas ativas (deleted_at IS NULL) de um paciente.
 * Pinned primeiro, depois desc por created_at.
 *
 * @param {string} patientId
 * @param {Object} [opts]
 * @param {string} [opts.tenantId]
 * @param {string|null} [opts.noteType]  - filtra por tipo (anamnese, evolucao_sessao, etc)
 * @param {boolean} [opts.includeDeleted=false]
 * @param {boolean} [opts.brief=false]   - usa SELECT brief sem content (lista/timeline)
 */
export async function listForPatient(patientId, { tenantId, noteType = null, includeDeleted = false, brief = false } = {}) {
    if (!patientId) return [];
    const tid = resolveTenantId(tenantId);
    const select = brief ? CLINICAL_NOTE_SELECT_BRIEF : CLINICAL_NOTE_SELECT;

    let q = tenantDb().from('clinical_notes')
        .select(select)
        
        .eq('patient_id', patientId)
        .order('pinned', { ascending: false })
        .order('created_at', { ascending: false });

    if (!includeDeleted) q = q.is('deleted_at', null);
    if (noteType) q = q.eq('note_type', noteType);

    const { data, error } = await q;
    if (error) throw error;
    return (data || []).map(flattenNoteRow);
}

/**
 * Lista notas vinculadas a uma sessão (session_event_id).
 * Útil pra mostrar "anotações desta sessão" no AgendaEventDialog.
 */
export async function listForSession(sessionEventId, { tenantId, brief = false } = {}) {
    if (!sessionEventId) return [];
    const tid = resolveTenantId(tenantId);
    const select = brief ? CLINICAL_NOTE_SELECT_BRIEF : CLINICAL_NOTE_SELECT;

    const { data, error } = await tenantDb().from('clinical_notes').select(select).eq('session_event_id', sessionEventId).is('deleted_at', null).order('created_at', { ascending: false });

    if (error) throw error;
    return (data || []).map(flattenNoteRow);
}

/**
 * Lê uma nota completa por id.
 */
export async function getById(noteId, { tenantId } = {}) {
    if (!noteId) throw new Error('ID inválido.');
    const tid = resolveTenantId(tenantId);

    const { data, error } = await tenantDb().from('clinical_notes').select(CLINICAL_NOTE_SELECT).eq('id', noteId).maybeSingle();

    if (error) throw error;
    return data ? flattenNoteRow(data) : null;
}

/**
 * Cria nota clínica. owner_id + tenant_id + created_by injetados pelo repository.
 * Trigger no banco cria automaticamente version_number=1.
 *
 * @param {Object} payload
 * @param {string} payload.patient_id    - obrigatório
 * @param {string} payload.note_type     - obrigatório (CHECK no banco)
 * @param {string} [payload.session_event_id]
 * @param {string} [payload.template_id]
 * @param {string} [payload.title]
 * @param {string} [payload.content_text]
 * @param {Object} [payload.content_structured]
 * @param {boolean} [payload.pinned=false]
 * @param {boolean} [payload.is_draft=false]
 */
export async function create(payload) {
    if (!payload?.patient_id) throw new Error('patient_id obrigatório.');
    if (!payload?.note_type) throw new Error('note_type obrigatório.');
    assertNoteType(payload.note_type);
    if (!payload.content_text && !payload.content_structured) {
        throw new Error('Nota precisa de content_text ou content_structured (CHECK constraint).');
    }

    const uid = await getUid();
    const tid = resolveTenantId();

    const row = {
        patient_id: payload.patient_id,
        owner_id: uid,
        tenant_id: tid,
        session_event_id: payload.session_event_id || null,
        note_type: payload.note_type,
        template_id: payload.template_id || null,
        title: payload.title ? String(payload.title).trim() || null : null,
        content_text: payload.content_text ? String(payload.content_text).trim() || null : null,
        content_structured: payload.content_structured || null,
        pinned: !!payload.pinned,
        is_draft: !!payload.is_draft,
        created_by: uid
    };

    const { data, error } = await tenantDb().from('clinical_notes').insert([row]).select(CLINICAL_NOTE_SELECT).single();

    if (error) throw error;
    return flattenNoteRow(data);
}

/**
 * Atualiza nota clínica. Repository injeta updated_by + updated_at é setado pelo trigger.
 * Trigger cria nova versão se content/title/deleted_at mudaram.
 *
 * @param {string} noteId
 * @param {Object} patch
 * @param {Object} [opts]
 * @param {string} [opts.tenantId]
 */
export async function update(noteId, patch, { tenantId } = {}) {
    if (!noteId) throw new Error('ID inválido.');
    const tid = resolveTenantId(tenantId);
    const uid = await getUid();

    if (patch?.note_type) assertNoteType(patch.note_type);

    const safePatch = { ...sanitize(patch), updated_by: uid };

    const { data, error } = await tenantDb().from('clinical_notes').update(safePatch).eq('id', noteId).select(CLINICAL_NOTE_SELECT).single();

    if (error) throw error;
    return flattenNoteRow(data);
}

/**
 * Soft delete — seta deleted_at + deleted_by. Trigger cria versão snapshot.
 * Hard delete bloqueado em RLS — só soft.
 */
export async function softDelete(noteId, { tenantId } = {}) {
    if (!noteId) throw new Error('ID inválido.');
    const tid = resolveTenantId(tenantId);
    const uid = await getUid();

    const { error } = await tenantDb().from('clinical_notes')
        .update({ deleted_at: new Date().toISOString(), deleted_by: uid, updated_by: uid })
        .eq('id', noteId)
        ;

    if (error) throw error;
    return true;
}

/**
 * Restore — clears deleted_at/deleted_by. Trigger cria versão snapshot com reason='restore'.
 */
export async function restore(noteId, { tenantId } = {}) {
    if (!noteId) throw new Error('ID inválido.');
    const tid = resolveTenantId(tenantId);
    const uid = await getUid();

    const { error } = await tenantDb().from('clinical_notes').update({ deleted_at: null, deleted_by: null, updated_by: uid }).eq('id', noteId);

    if (error) throw error;
    return true;
}

/**
 * Toggle pinned (utilitário comum).
 */
export async function setPinned(noteId, pinned, { tenantId } = {}) {
    return update(noteId, { pinned: !!pinned }, { tenantId });
}

/**
 * Lista versões (audit trail) de uma nota. Ordem desc por version_number.
 */
export async function listVersions(noteId) {
    if (!noteId) return [];

    const { data, error } = await tenantDb().from('clinical_note_versions').select(CLINICAL_NOTE_VERSION_SELECT).eq('note_id', noteId).order('version_number', { ascending: false });

    if (error) throw error;
    return data || [];
}

/**
 * Lê snapshot de uma versão específica.
 */
export async function getVersion(noteId, versionNumber) {
    if (!noteId) throw new Error('noteId obrigatório.');
    if (!versionNumber) throw new Error('versionNumber obrigatório.');

    const { data, error } = await tenantDb().from('clinical_note_versions').select(CLINICAL_NOTE_VERSION_SELECT).eq('note_id', noteId).eq('version_number', versionNumber).maybeSingle();

    if (error) throw error;
    return data || null;
}

// ─── helpers internos ────────────────────────────────────────────────────────

function sanitize(patch) {
    const out = { ...patch };
    if ('title' in out && typeof out.title === 'string') {
        const t = out.title.trim();
        out.title = t === '' ? null : t;
    }
    if ('content_text' in out && typeof out.content_text === 'string') {
        const t = out.content_text.trim();
        out.content_text = t === '' ? null : t;
    }
    return out;
}
