<?php

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

return new class extends Migration
{
    public function up(): void
    {
        $expiredReservationIds = fn ($query) => $query
            ->select('id')
            ->from('stock_reservations')
            ->where('status', 'expired');

        DB::table('compras')
            ->whereIn('status', ['created', 'pending_payment'])
            ->whereIn('stock_reservation_id', $expiredReservationIds)
            ->update(['status' => 'expired']);

        DB::table('carritos')
            ->whereIn('status', ['active', 'checkout'])
            ->whereIn('current_stock_reservation_id', $expiredReservationIds)
            ->update(['status' => 'expired']);
    }

    public function down(): void
    {
        // Terminal business states cannot be reversed without inventing their prior state.
    }
};
