<?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('treatments', function (Blueprint $table) {
            $table->id('treatment_id');
            $table->foreignId('row_id')->constrained('vineyard_rows')->cascadeOnDelete();
            $table->text('note')->nullable();
            $table->timestamps();
        });

        Schema::create('waterings', function (Blueprint $table) {
            $table->unsignedBigInteger('treatment_id')->primary();
            $table->unsignedInteger('time_interval')->nullable();
            $table->decimal('amount', 8, 2)->nullable();
            $table->text('note')->nullable();
            $table->timestamps();

            $table->foreign('treatment_id')
                ->references('treatment_id')
                ->on('treatments')
                ->cascadeOnDelete();
        });

        Schema::create('fertilizations', function (Blueprint $table) {
            $table->unsignedBigInteger('treatment_id')->primary();
            $table->string('substance');
            $table->decimal('concentration', 8, 2)->nullable();
            $table->text('note')->nullable();
            $table->timestamps();

            $table->foreign('treatment_id')
                ->references('treatment_id')
                ->on('treatments')
                ->cascadeOnDelete();
        });

        Schema::create('sprayings', function (Blueprint $table) {
            $table->unsignedBigInteger('treatment_id')->primary();
            $table->string('pesticide');
            $table->decimal('concentration', 8, 2)->nullable();
            $table->text('note')->nullable();
            $table->timestamps();

            $table->foreign('treatment_id')
                ->references('treatment_id')
                ->on('treatments')
                ->cascadeOnDelete();
        });

        Schema::create('prunings', function (Blueprint $table) {
            $table->unsignedBigInteger('treatment_id')->primary();
            $table->string('method');
            $table->unsignedInteger('percentage_removed')->nullable();
            $table->text('note')->nullable();
            $table->timestamps();

            $table->foreign('treatment_id')
                ->references('treatment_id')
                ->on('treatments')
                ->cascadeOnDelete();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('prunings');
        Schema::dropIfExists('sprayings');
        Schema::dropIfExists('fertilizations');
        Schema::dropIfExists('waterings');
        Schema::dropIfExists('treatments');
    }
};
