USE ficha_medica;

CREATE TABLE IF NOT EXISTS patients (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(120) NOT NULL,
  active TINYINT(1) NOT NULL DEFAULT 1,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY uk_patient_name (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT IGNORE INTO patients (id, name, active) VALUES (1, 'Yiyita', 1);

ALTER TABLE medical_followups
  ADD COLUMN IF NOT EXISTS patient_id INT NULL AFTER user_id;

UPDATE medical_followups SET patient_id = 1 WHERE patient_id IS NULL;

ALTER TABLE medical_followups
  MODIFY patient_id INT NOT NULL;

ALTER TABLE medical_followups
  ADD INDEX IF NOT EXISTS idx_followup_patient (patient_id);
