<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('cost_unit_types', function (Blueprint $table) {
            $table->string('slug')->primary();
            $table->string('name');
            $table->timestamps();
        });

        Schema::create('cost_units', function (Blueprint $table) {
            $table->id();
            $table->string('tenant');
            $table->string('name');
            $table->string('type');
            $table->dateTime('billing_deadline')->nullable();
            $table->float('distance_allowance');
            $table->boolean('mail_on_new')->default(true);
            $table->boolean('allow_new')->default(true);
            $table->boolean('archived')->default(false);

            $table->foreign('tenant')->references('slug')->on('tenants')->restrictOnDelete()->cascadeOnUpdate();
            $table->foreign('type')->references('slug')->on('cost_unit_types')->restrictOnDelete()->cascadeOnUpdate();
            $table->timestamps();
        });

        Schema::create('cost_unit_treasurers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('cost_unit_id')->constrained('cost_units', 'id')->restrictOnDelete()->cascadeOnUpdate();
            $table->foreignId('user_id')->constrained('users', 'id')->restrictOnDelete()->cascadeOnUpdate();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('cost_unit_treasurers');
        Schema::dropIfExists('cost_units');
        Schema::dropIfExists('cost_unit_types');
    }
};
