<?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('finance_cost_entries', function (Blueprint $table) {
            $table->string('id', 120)->primary();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->date('date');
            $table->string('category', 30);
            $table->string('subcategory');
            $table->decimal('amount_crc', 14, 2)->default(0);
            $table->decimal('quantity', 14, 3)->nullable();
            $table->string('unit', 20)->nullable();
            $table->decimal('unit_cost_crc', 14, 4)->nullable();
            $table->string('source', 20)->default('MANUAL');
            $table->json('ref_json')->nullable();
            $table->json('scope_json')->nullable();
            $table->text('notes')->nullable();
            $table->timestamp('created_at_client')->nullable();
            $table->timestamps();

            $table->index(['user_id', 'date']);
            $table->index(['user_id', 'category']);
        });
    }

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