<?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
    {
        if (Schema::hasTable('monthly_sales_tracking')) return;
        
        Schema::create('monthly_sales_tracking', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->onDelete('cascade');
            $table->integer('year');
            $table->integer('month');
            $table->decimal('total_purchase_amount', 15, 2)->default(0);
            $table->decimal('personal_shop_commission', 15, 2)->default(0);
            $table->boolean('has_met_amm')->default(false);
            $table->boolean('is_eligible_for_commissions')->default(false);
            $table->timestamps();

            $table->unique(['user_id', 'year', 'month']);
            $table->index(['year', 'month']);
        });
    }

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

