<?php

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

return new class extends Migration
{
    public function up(): void
    {
        /*
        |--------------------------------------------------------------------------
        | APAGAR TODAS AS TABELAS
        |--------------------------------------------------------------------------
        */

        Schema::disableForeignKeyConstraints();

        Schema::dropIfExists('alunos');
        Schema::dropIfExists('turmas');
        Schema::dropIfExists('user_escolas');
        Schema::dropIfExists('escolas');
        Schema::dropIfExists('users');

        Schema::enableForeignKeyConstraints();


        /*
        |--------------------------------------------------------------------------
        | CRIAR TABELA ESCOLAS
        |--------------------------------------------------------------------------
        */

        Schema::create('escolas', function (Blueprint $table) {

            $table->id();

            $table->string('cnpj')->unique();

            $table->string('nome');

            $table->json('endereco')->nullable();

            $table->timestamps();

        });


        /*
        |--------------------------------------------------------------------------
        | CRIAR USERS
        |--------------------------------------------------------------------------
        */

        Schema::create('users', function (Blueprint $table) {

            $table->id();

            $table->string('name');

            $table->string('email')->unique();

            $table->string('password');

            $table->boolean('is_super_admin')
                ->default(false);

            $table->foreignId('current_escola_id')
                ->nullable()
                ->constrained('escolas')
                ->nullOnDelete();

            $table->rememberToken();

            $table->timestamps();

        });


        /*
        |--------------------------------------------------------------------------
        | TABELA PIVOT USER_ESCOLAS
        |--------------------------------------------------------------------------
        */

        Schema::create('user_escolas', function (Blueprint $table) {

            $table->id();

            $table->foreignId('user_id')
                ->constrained()
                ->cascadeOnDelete();

            $table->foreignId('escola_id')
                ->constrained()
                ->cascadeOnDelete();

            $table->timestamps();

        });


        /*
        |--------------------------------------------------------------------------
        | TABELA TURMAS
        |--------------------------------------------------------------------------
        */

        Schema::create('turmas', function (Blueprint $table) {

            $table->id();

            $table->foreignId('id_escola')
                ->constrained('escolas')
                ->cascadeOnDelete();

            $table->string('nome');

            $table->text('descricao')
                ->nullable();

            $table->timestamps();

        });


        /*
        |--------------------------------------------------------------------------
        | TABELA ALUNOS
        |--------------------------------------------------------------------------
        */

        Schema::create('alunos', function (Blueprint $table) {

            $table->id();

            $table->foreignId('id_escola')
                ->constrained('escolas')
                ->cascadeOnDelete();

            $table->foreignId('id_turma')
                ->constrained('turmas')
                ->cascadeOnDelete();

            $table->string('nome');

            $table->string('cpf')
                ->nullable();

            $table->date('data_nascimento')
                ->nullable();

            $table->date('data_inscricao')
                ->nullable();

            $table->date('data_encerramento')
                ->nullable();

            $table->json('endereco')
                ->nullable();

            $table->timestamps();

        });

    }


    public function down(): void
    {

        Schema::disableForeignKeyConstraints();

        Schema::dropIfExists('alunos');
        Schema::dropIfExists('turmas');
        Schema::dropIfExists('user_escolas');
        Schema::dropIfExists('users');
        Schema::dropIfExists('escolas');

        Schema::enableForeignKeyConstraints();

    }
};
