<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('plan_usages', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('plan_id')->constrained()->onDelete('cascade');
            $table->foreignId('plan_feature_id')->constrained()->onDelete('cascade');
            $table->decimal('usage_amount', 10, 2)->default(0);
            $table->string('usage_type')->default('monthly'); // monthly, daily, total
            $table->date('period_start'); // Usage period start date
            $table->date('period_end'); // Usage period end date
            $table->json('metadata')->nullable();
            $table->timestamps();

            $table->unique(['user_id', 'plan_feature_id', 'usage_type', 'period_start'], 'plan_usage_unique');
            $table->index(['user_id', 'period_start']);
            $table->index(['plan_feature_id', 'period_start']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('plan_usages');
    }
};
