<?php

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

return new class extends Migration {
    public function up(): void
    {
        Schema::table('serenata_packages', function (Blueprint $table) {
            $table->decimal('commission_amount', 12, 2)->default(0)->after('internal_cost');
        });

        $driver = Schema::getConnection()->getDriverName();
        $expression = $driver === 'sqlite'
            ? 'CASE WHEN (base_price - internal_cost) > 0 THEN (base_price - internal_cost) ELSE 0 END'
            : 'GREATEST(base_price - internal_cost, 0)';

        DB::table('serenata_packages')->update([
            'commission_amount' => DB::raw($expression),
        ]);
    }

    public function down(): void
    {
        Schema::table('serenata_packages', function (Blueprint $table) {
            $table->dropColumn('commission_amount');
        });
    }
};
