<?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_consumables', function (Blueprint $table) {
            if (! Schema::hasColumn('report_consumables', 'unit_price')) {
                $table->decimal('unit_price', 15, 0)->nullable()->after('quantity');
            }
        });

        if (! Schema::hasTable('health_treatments')) {
            Schema::create('health_treatments', function (Blueprint $table) {
                $table->id();
                $table->foreignId('project_id')->constrained('projects')->cascadeOnDelete();
                $table->foreignId('product_id')->nullable()->constrained('products')->nullOnDelete();
                $table->foreignId('producer_id')->nullable()->constrained('producers')->nullOnDelete();
                $table->foreignId('initial_report_id')->nullable()->constrained('reports')->nullOnDelete();
                $table->string('product_code', 100)->nullable();
                $table->string('product_name')->nullable();
                $table->string('breed_name')->nullable();
                $table->string('disease_name')->nullable();
                $table->string('status', 40)->default('under_treatment');
                $table->unsignedTinyInteger('progress_percent')->default(0);
                $table->decimal('weight', 15, 3)->nullable();
                $table->timestamp('started_at')->nullable();
                $table->timestamp('ended_at')->nullable();
                $table->date('next_visit_at')->nullable();
                $table->json('attachments')->nullable();
                $table->text('notes')->nullable();
                $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
                $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
                $table->timestamps();
                $table->softDeletes();

                $table->index(['project_id', 'status']);
                $table->index(['product_id', 'status']);
            });
        }

        if (! Schema::hasTable('health_treatment_updates')) {
            Schema::create('health_treatment_updates', function (Blueprint $table) {
                $table->id();
                $table->foreignId('health_treatment_id')->constrained('health_treatments')->cascadeOnDelete();
                $table->timestamp('report_at')->nullable();
                $table->unsignedTinyInteger('progress_percent')->nullable();
                $table->text('notes')->nullable();
                $table->text('expert_opinion')->nullable();
                $table->text('written_report')->nullable();
                $table->json('attachments')->nullable();
                $table->json('documents')->nullable();
                $table->decimal('total_cost', 15, 0)->nullable();
                $table->string('invoice_image')->nullable();
                $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
                $table->timestamps();
            });
        }

        if (! Schema::hasTable('health_treatment_consumables')) {
            Schema::create('health_treatment_consumables', function (Blueprint $table) {
                $table->id();
                $table->foreignId('health_treatment_update_id')
                    ->constrained('health_treatment_updates', indexName: 'ht_consumables_update_fk')
                    ->cascadeOnDelete();
                $table->string('name');
                $table->string('unit', 50)->nullable();
                $table->decimal('quantity', 15, 3)->default(0);
                $table->decimal('unit_price', 15, 0)->nullable();
                $table->integer('sort_order')->default(0);
                $table->timestamps();
            });
        }

        if (! Schema::hasTable('health_treatment_invoice_items')) {
            Schema::create('health_treatment_invoice_items', function (Blueprint $table) {
                $table->id();
                $table->foreignId('health_treatment_update_id')
                    ->constrained('health_treatment_updates', indexName: 'ht_invoice_items_update_fk')
                    ->cascadeOnDelete();
                $table->string('title');
                $table->decimal('amount', 15, 0)->default(0);
                $table->integer('sort_order')->default(0);
                $table->timestamps();
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('health_treatment_invoice_items');
        Schema::dropIfExists('health_treatment_consumables');
        Schema::dropIfExists('health_treatment_updates');
        Schema::dropIfExists('health_treatments');

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