-- 006 (L13-L14): vínculo usuario-cirujano, agenda de citas y consentimientos
CREATE TABLE IF NOT EXISTS agenda_slots (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  cirujano_id BIGINT UNSIGNED NOT NULL,
  inicio DATETIME NOT NULL,
  duracion_min SMALLINT NOT NULL DEFAULT 20,
  modalidad ENUM('presencial','video') DEFAULT 'presencial',
  estado ENUM('libre','reservado','bloqueado') NOT NULL DEFAULT 'libre',
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY uq_slot (cirujano_id, inicio)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS citas (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  slot_id BIGINT UNSIGNED NOT NULL UNIQUE,
  user_id BIGINT UNSIGNED NOT NULL,
  motivo VARCHAR(300) NULL,
  estado ENUM('confirmada','cancelada','completada') NOT NULL DEFAULT 'confirmada',
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  KEY idx_cita_u (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS consent_plantillas (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  cirujano_id BIGINT UNSIGNED NULL,
  titulo VARCHAR(200) NOT NULL,
  cuerpo MEDIUMTEXT NOT NULL,
  version SMALLINT NOT NULL DEFAULT 1,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS consentimientos (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  plantilla_id BIGINT UNSIGNED NOT NULL,
  cirujano_id BIGINT UNSIGNED NOT NULL,
  user_id BIGINT UNSIGNED NOT NULL,
  estado ENUM('enviado','firmado','rechazado') NOT NULL DEFAULT 'enviado',
  firmado_at DATETIME NULL,
  firma_hash CHAR(64) NULL,
  firma_ip VARCHAR(45) NULL,
  cuerpo_congelado MEDIUMTEXT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  KEY idx_cons (user_id, estado), KEY idx_cons_c (cirujano_id, estado)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
