<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

/**
 * Add database-level CHECK constraint on price_pence (MySQL only).
 *
 * Range: 5 000–50 000 = 50p–500p per litre.
 * This is a broad last-resort guard; the application layer enforces
 * tighter per-fuel-type limits before any insert reaches the DB.
 */
return new class extends Migration
{
    public function up(): void
    {
        if (DB::getDriverName() !== 'mysql') {
            return;
        }

        DB::statement('ALTER TABLE station_prices ADD CONSTRAINT chk_price_pence_range CHECK (price_pence BETWEEN 5000 AND 50000)');
        DB::statement('ALTER TABLE station_prices_current ADD CONSTRAINT chk_current_price_pence_range CHECK (price_pence BETWEEN 5000 AND 50000)');
    }

    public function down(): void
    {
        if (DB::getDriverName() !== 'mysql') {
            return;
        }

        DB::statement('ALTER TABLE station_prices DROP CHECK chk_price_pence_range');
        DB::statement('ALTER TABLE station_prices_current DROP CHECK chk_current_price_pence_range');
    }
};
