<?php

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

class CreateTrainingSessionsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('training_sessions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger("box_id");
            $table->unsignedBigInteger("coach_id")->nullable();
            $table->unsignedBigInteger("training_room_id")->nullable();
            $table->unsignedBigInteger("training_workout_id")->nullable();
            $table->unsignedBigInteger("training_type_id")->nullable();
            $table->date("day")->index();
            $table->time("start_time");
            $table->time("end_time");
            $table->integer("capacity");
            $table->boolean("credits")->default(true);
            $table->timestamps();

            $table->foreign("box_id")->references("id")->on("boxes")->onDelete("cascade");
            $table->foreign("coach_id")->references("id")->on("users")->onDelete("set null");
            $table->foreign("training_room_id")->references("id")->on("training_rooms")->onDelete("set null"); //si se borra training rooms, no es borrin les seves training sessions
            $table->foreign("training_workout_id")->references("id")->on("training_workouts")->onDelete("restrict");
            $table->foreign("training_type_id")->references("id")->on("training_types")->onDelete("set null");
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('training_sessions');
    }
}
