<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('warehouse_stock', function (Blueprint $table) {
            $table->unsignedBigInteger('sell_price')->nullable()->after('min_stock');
        });

        // Migrate existing sell_price from products to warehouse_stock
        DB::statement('
            UPDATE warehouse_stock
            SET sell_price = (
                SELECT products.sell_price
                FROM products
                WHERE products.id = warehouse_stock.product_id
            )
        ');
    }

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