-- PARCHE OPCIONAL V3 - CAMPOS DE HORAS EN NOVEDADES DEL PERIODO
-- Ejecutar solo si el sistema no logra crear las columnas automáticamente.
-- MySQL 8 compatible.

SET @db := DATABASE();

SET @sql := (
    SELECT IF(
        COUNT(*) = 0,
        'ALTER TABLE novedades_periodo ADD COLUMN he_diurna DECIMAL(10,2) NOT NULL DEFAULT 0.00 AFTER horas_retardo_periodo',
        'SELECT "he_diurna ya existe" AS info'
    )
    FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'novedades_periodo' AND COLUMN_NAME = 'he_diurna'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql := (
    SELECT IF(
        COUNT(*) = 0,
        'ALTER TABLE novedades_periodo ADD COLUMN he_nocturna DECIMAL(10,2) NOT NULL DEFAULT 0.00 AFTER he_diurna',
        'SELECT "he_nocturna ya existe" AS info'
    )
    FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'novedades_periodo' AND COLUMN_NAME = 'he_nocturna'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql := (
    SELECT IF(
        COUNT(*) = 0,
        'ALTER TABLE novedades_periodo ADD COLUMN horas_nocturnas_ordinarias DECIMAL(10,2) NOT NULL DEFAULT 0.00 AFTER he_nocturna',
        'SELECT "horas_nocturnas_ordinarias ya existe" AS info'
    )
    FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'novedades_periodo' AND COLUMN_NAME = 'horas_nocturnas_ordinarias'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Verificación final
SHOW COLUMNS FROM novedades_periodo WHERE Field IN ('he_diurna','he_nocturna','horas_nocturnas_ordinarias');
