<?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('customer_ratings', function (Blueprint $table) {
            $table->id();
            $table->foreignId('customer_id')->constrained()->onDelete('cascade');
            $table->enum('risk_level', ['low', 'medium', 'high', 'critical'])->default('low');
            $table->enum('status', ['active', 'suspended', 'blacklisted'])->default('active');
            $table->integer('violation_count')->default(0);
            $table->integer('late_payment_count')->default(0);
            $table->decimal('total_late_amount', 12, 2)->default(0);
            $table->integer('rental_count')->default(0);
            $table->decimal('average_rating', 3, 1)->nullable();
            $table->text('notes')->nullable();
            $table->dateTime('last_violation_date')->nullable();
            $table->dateTime('last_late_payment_date')->nullable();
            $table->foreignId('updated_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamps();
            $table->index('customer_id');
            $table->index('risk_level');
            $table->index('status');
        });
    }

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