<?php

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

return new class extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('products', function (Blueprint $table) {
            $table
                ->bigInteger('product_category_id')
                ->unsigned()
                ->after('id');
            $table
                ->string('name')
                ->unique()
                ->after('product_category_id');
            $table->string('package')->after('name');
            $table
                ->double('purch_price')
                ->nullable()
                ->after('package');
            $table
                ->double('sell_price')
                ->nullable()
                ->after('purch_price');
            $table
                ->double('stock')
                ->nullable()
                ->after('sell_price');
            $table
                ->text('notes')
                ->nullable()
                ->after('stock');
            $table
                ->string('photo')
                ->nullable()
                ->after('notes');
            $table
                ->bigInteger('sale_id')
                ->unsigned()
                ->after('photo');
            $table
                ->bigInteger('purchase_id')
                ->unsigned()
                ->after('sale_id');
            $table
                ->foreign('product_category_id')
                ->references('id')
                ->on('product_categories')
                ->onDelete('cascade')
                ->onUpdate('cascade');
            $table
                ->foreign('sale_id')
                ->references('id')
                ->on('sales')
                ->onDelete('cascade')
                ->onUpdate('cascade');
            $table
                ->foreign('purchase_id')
                ->references('id')
                ->on('purchases')
                ->onDelete('cascade')
                ->onUpdate('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('products', function (Blueprint $table) {
            $table->dropColumn('product_category_id');
            $table->dropColumn('name');
            $table->dropColumn('package');
            $table->dropColumn('purch_price');
            $table->dropColumn('sell_price');
            $table->dropColumn('stock');
            $table->dropColumn('notes');
            $table->dropColumn('photo');
            $table->dropColumn('sale_id');
            $table->dropColumn('purchase_id');
            $table->dropForeign('products_product_category_id_foreign');
            $table->dropForeign('products_sale_id_foreign');
            $table->dropForeign('products_purchase_id_foreign');
        });
    }
};
