<?php

use App\Models\OrderManagement\Order;
use App\Models\OrderManagement\OrderProduct;
use App\Models\ProductManagement\Product;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up(){
        Schema::create((new OrderProduct)->getTable(), function (Blueprint $table) {
            $table->id();
            $table->foreignIdFor(Order::class,'order_id')
                ->constrained((new Order)->getTable(), (new Order)->getKeyName())
                ->cascadeOnUpdate()->cascadeOnDelete()
                ->comment("[FK]");

            $table->foreignIdFor(Product::class,'product_id')
                ->constrained((new Product)->getTable(), (new Product)->getKeyName())
                ->cascadeOnUpdate()->cascadeOnDelete()
                ->comment("[FK]");

            $table->float('quantity');

            $table->float('price')->nullable()
                  ->comment("Le prix unitaire");

            $table->float('amount')->storedAs("price * quantity")
                ->comment("Le montant");

//            $table->float('amount2')->virtualAs("price * quantity")
//                ->comment("Le montant2");

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down(){
        Schema::dropIfExists((new OrderProduct)->getTable());
    }
};
