<?php

declare(strict_types=1);

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('report_items', function (Blueprint $table) {
            $table->string('group_name', 120)->nullable()->after('product_id');
            $table->decimal('avg_value', 15, 3)->nullable()->after('value');
            $table->text('action_note')->nullable()->after('avg_value');
        });

        Schema::table('reports', function (Blueprint $table) {
            if (! Schema::hasColumn('reports', 'create_invoice')) {
                $table->boolean('create_invoice')->default(false)->after('documents');
            }
        });
    }

    public function down(): void
    {
        Schema::table('report_items', function (Blueprint $table) {
            $table->dropColumn(['group_name', 'avg_value', 'action_note']);
        });

        Schema::table('reports', function (Blueprint $table) {
            if (Schema::hasColumn('reports', 'create_invoice')) {
                $table->dropColumn('create_invoice');
            }
        });
    }
};
