<?php

use App\Models\ProductManagement\Product;
use App\Models\ProductManagement\ProductHasTag;
use App\Models\ProductManagement\Tag;
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 ProductHasTag)->getTable(), function (Blueprint $table) {

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


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

            $table->primary(['product_id', 'tag_id'], 'product_tag_primary');
        });
    }

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