<?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('monthly_company_financials', function (Blueprint $table) {
            $table->id();
            $table->foreignId('company_id')->constrained('companies')->onDelete('cascade');
            $table->string('year_month', 7); // YYYY-MM
            $table->decimal('revenue', 15, 2)->default(0);
            $table->decimal('expenses', 15, 2)->default(0);
            $table->decimal('loan_deductions', 15, 2)->default(0);
            $table->decimal('net_profit', 15, 2)->default(0);
            $table->decimal('profit_swept', 15, 2)->default(0);
            $table->decimal('available_profit', 15, 2)->default(0);
            $table->timestamps();

            $table->unique(['company_id', 'year_month']);
            $table->index(['company_id', 'year_month']);
        });
    }

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