-- Migration v82: Adiciona agendamento_id (FK master_agendamentos) em todas as tabelas clínicas
-- Permite vincular qualquer registro clínico a uma consulta ambulatorial
-- internacao_id permanece para atendimentos hospitalares
-- Nota: ADD COLUMN IF NOT EXISTS e ADD INDEX IF NOT EXISTS separados por tabela para compatibilidade MariaDB.

ALTER TABLE sadt_solicitacoes
    ADD COLUMN IF NOT EXISTS agendamento_id INT NULL DEFAULT NULL AFTER internacao_id;
ALTER TABLE sadt_solicitacoes
    ADD INDEX IF NOT EXISTS idx_sadt_agendamento (agendamento_id);

ALTER TABLE farmacia_dispensacoes
    ADD COLUMN IF NOT EXISTS agendamento_id INT NULL DEFAULT NULL AFTER internacao_id;
ALTER TABLE farmacia_dispensacoes
    ADD INDEX IF NOT EXISTS idx_farmacia_disp_agendamento (agendamento_id);

ALTER TABLE hospital_prescricoes
    ADD COLUMN IF NOT EXISTS agendamento_id INT NULL DEFAULT NULL AFTER internacao_id;
ALTER TABLE hospital_prescricoes
    ADD INDEX IF NOT EXISTS idx_prescricao_agendamento (agendamento_id);

ALTER TABLE enfermagem_evolucoes
    ADD COLUMN IF NOT EXISTS agendamento_id INT NULL DEFAULT NULL AFTER internacao_id;
ALTER TABLE enfermagem_evolucoes
    ADD INDEX IF NOT EXISTS idx_enf_evolucao_agendamento (agendamento_id);

ALTER TABLE enfermagem_sinais_vitais
    ADD COLUMN IF NOT EXISTS agendamento_id INT NULL DEFAULT NULL AFTER internacao_id;
ALTER TABLE enfermagem_sinais_vitais
    ADD INDEX IF NOT EXISTS idx_enf_sv_agendamento (agendamento_id);

ALTER TABLE ccih_notificacoes
    ADD COLUMN IF NOT EXISTS agendamento_id INT NULL DEFAULT NULL;
ALTER TABLE ccih_notificacoes
    ADD INDEX IF NOT EXISTS idx_ccih_agendamento (agendamento_id);

ALTER TABLE `triagem_manchester`
    ADD COLUMN IF NOT EXISTS agendamento_id INT NULL DEFAULT NULL;
ALTER TABLE `triagem_manchester`
    ADD INDEX IF NOT EXISTS idx_triagem_agendamento (agendamento_id);

ALTER TABLE `lis_exames`
    ADD COLUMN IF NOT EXISTS agendamento_id INT NULL DEFAULT NULL AFTER internacao_id;
ALTER TABLE `lis_exames`
    ADD INDEX IF NOT EXISTS idx_lis_agendamento (agendamento_id);

ALTER TABLE `pacs_laudos`
    ADD COLUMN IF NOT EXISTS agendamento_id INT NULL DEFAULT NULL AFTER internacao_id;
ALTER TABLE `pacs_laudos`
    ADD INDEX IF NOT EXISTS idx_pacs_agendamento (agendamento_id);

ALTER TABLE nutricao_dietas
    ADD COLUMN IF NOT EXISTS agendamento_id INT NULL DEFAULT NULL;
ALTER TABLE nutricao_dietas
    ADD INDEX IF NOT EXISTS idx_nutricao_dieta_agendamento (agendamento_id);

ALTER TABLE nutricao_avaliacoes
    ADD COLUMN IF NOT EXISTS agendamento_id INT NULL DEFAULT NULL;
ALTER TABLE nutricao_avaliacoes
    ADD INDEX IF NOT EXISTS idx_nutricao_aval_agendamento (agendamento_id);

ALTER TABLE cc_cirurgias
    ADD COLUMN IF NOT EXISTS agendamento_id INT NULL DEFAULT NULL;
ALTER TABLE cc_cirurgias
    ADD INDEX IF NOT EXISTS idx_cc_agendamento (agendamento_id);

ALTER TABLE hospital_sumario_alta
    ADD COLUMN IF NOT EXISTS agendamento_id INT NULL DEFAULT NULL;
ALTER TABLE hospital_sumario_alta
    ADD INDEX IF NOT EXISTS idx_sumario_agendamento (agendamento_id);

ALTER TABLE balanco_hidrico
    ADD COLUMN IF NOT EXISTS agendamento_id INT NULL DEFAULT NULL;
ALTER TABLE balanco_hidrico
    ADD INDEX IF NOT EXISTS idx_balanco_agendamento (agendamento_id);

ALTER TABLE `aih_sus`
    ADD COLUMN IF NOT EXISTS agendamento_id INT NULL DEFAULT NULL AFTER internacao_id;
ALTER TABLE `aih_sus`
    ADD INDEX IF NOT EXISTS idx_aih_agendamento (agendamento_id);

ALTER TABLE prontuarios
    ADD COLUMN IF NOT EXISTS agendamento_id INT NULL DEFAULT NULL;
ALTER TABLE prontuarios
    ADD INDEX IF NOT EXISTS idx_prontuario_agendamento (agendamento_id);
