-- ============================================================
-- Migration: 20260302_lab_05_asignaciones
-- Módulo Administrativo: Asignación de enfermera a domicilio
-- ============================================================

CREATE TABLE IF NOT EXISTS `lab_asignaciones` (
    `id`                INT(11)         NOT NULL AUTO_INCREMENT,
    `domicilio_id`      INT(11)         NOT NULL                COMMENT 'FK a lab_domicilios.id',
    `enfermera_id`      INT(11)         NOT NULL                COMMENT 'FK a lab_enfermeras.id',
    `asignada_por`      INT(11)         DEFAULT NULL            COMMENT 'FK a admin_users.id',
    `estado`            ENUM(
                            'asignada',
                            'confirmada',
                            'liberada',
                            'completada'
                        ) NOT NULL DEFAULT 'asignada',
    `notas`             TEXT            DEFAULT NULL,
    `created_at`        TIMESTAMP       NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at`        TIMESTAMP       NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    -- Solo una enfermera activa por domicilio
    UNIQUE KEY `uq_asig_domicilio` (`domicilio_id`),
    KEY `idx_asig_enfermera`    (`enfermera_id`),
    KEY `idx_asig_estado`       (`estado`),
    KEY `idx_asig_asignada`     (`asignada_por`),
    CONSTRAINT `fk_asig_domicilio`  FOREIGN KEY (`domicilio_id`)
        REFERENCES `lab_domicilios` (`id`) ON DELETE CASCADE,
    CONSTRAINT `fk_asig_enfermera`  FOREIGN KEY (`enfermera_id`)
        REFERENCES `lab_enfermeras` (`id`) ON DELETE RESTRICT,
    CONSTRAINT `fk_asig_admin`      FOREIGN KEY (`asignada_por`)
        REFERENCES `admin_users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  COMMENT='Asignación de enfermeras a domicilios';
