<?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
    {
        Schema::create('analysis_results', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id')->nullable()->index();

            // Data Perusahaan
            $table->string('company_name');
            $table->string('business_field');
            $table->string('other_field')->nullable();
            $table->string('period');
            $table->string('currency', 10)->default('IDR');

            // Data Keuangan (disimpan sebagai JSON)
            $table->json('financial_data');

            // Komponen Z-Score
            $table->decimal('x1', 12, 6)->default(0);
            $table->decimal('x2', 12, 6)->default(0);
            $table->decimal('x3', 12, 6)->default(0);
            $table->decimal('x4', 12, 6)->default(0);
            $table->decimal('x5', 12, 6)->default(0);
            $table->decimal('z_score', 12, 6)->default(0);

            // safe | grey | distress
            $table->string('z_category', 20)->default('grey');

            // Fitur tambahan
            $table->boolean('is_favorite')->default(false);

            $table->timestamps();

            // Foreign key ke users (nullable, opsional)
            $table->foreign('user_id')->references('id')->on('users')->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('analysis_results');
    }
};