<?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('daily_analytics', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id');
            $table->date('analytics_date');
            $table->integer('total_bookings')->default(0);
            $table->decimal('total_revenue', 12, 2)->default(0);
            $table->integer('total_drivers_active')->default(0);
            $table->integer('total_passengers_active')->default(0);
            $table->decimal('average_ride_rating', 3, 2)->nullable();
            $table->integer('average_ride_time_minutes')->nullable();
            $table->decimal('cancellation_rate', 5, 2)->nullable();
            $table->timestamps();
            
            $table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('cascade');
            $table->unique(['tenant_id', 'analytics_date']);
            $table->index('tenant_id');
            $table->index('analytics_date');
        });
    }

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