<?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('emplettes', function (Blueprint $table) {
            $table->timestamp('quote_sent_at')->nullable()->after('order_id');
        });

        // Données existantes : si le flux est déjà passé par un devis / suite, on verrouille rétroactivement.
        DB::table('emplettes')
            ->whereNull('quote_sent_at')
            ->whereIn('status', ['quoted', 'client_review', 'accepted', 'rejected', 'converted'])
            ->update(['quote_sent_at' => DB::raw('updated_at')]);
    }

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