<?php

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

class CreateTechniciansTable extends Migration
{
    public function up()
    {
        Schema::create('technicians', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('supplier_id');
            $table->timestamps();
            $table->unique(['user_id', 'supplier_id']);
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('supplier_id')->references('id')->on('suppliers')->onDelete('cascade');
        });

        Schema::table('finished_contracts', function (Blueprint $table) {
            $table->foreign('technical_id')->references('id')->on('technicians')->onDelete('set null');
        });

        Schema::table('current_contracts', function (Blueprint $table) {
            $table->foreign('technical_id')->references('id')->on('technicians')->onDelete('set null');
        });
    }

    public function down()
    {
        Schema::table('finished_contracts', function (Blueprint $table) {
            $table->dropForeign(['technical_id']);
        });

        Schema::table('current_contracts', function (Blueprint $table) {
            $table->dropForeign(['technical_id']);
        });

        Schema::dropIfExists('technicians');
    }
}
