<?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('trust_scores', function (Blueprint $table) {
            $table->id();
            $table->foreignId('media_owner_id')->constrained('media_owners')->onDelete('cascade');
            $table->foreignId('placement_id')->nullable()->constrained('placements')->onDelete('cascade');

            // Overall score
            $table->decimal('score', 4, 2)->default(0.00); // 0.00 - 5.00

            // Component scores
            $table->decimal('proof_timeliness_score', 4, 2)->default(0.00);
            $table->decimal('proof_pass_rate_score', 4, 2)->default(0.00);
            $table->decimal('dispute_score', 4, 2)->default(0.00);
            $table->decimal('cancellation_score', 4, 2)->default(0.00);

            // Raw counts used for scoring
            $table->unsignedInteger('total_bookings')->default(0);
            $table->unsignedInteger('proof_on_time')->default(0);
            $table->unsignedInteger('proof_late')->default(0);
            $table->unsignedInteger('proof_missed')->default(0);
            $table->unsignedInteger('proof_passed')->default(0);
            $table->unsignedInteger('proof_failed')->default(0);
            $table->unsignedInteger('disputes_raised')->default(0);
            $table->unsignedInteger('disputes_lost')->default(0);
            $table->unsignedInteger('cancellations')->default(0);

            // Eligibility
            $table->boolean('instant_book_eligible')->default(false);
            $table->timestamp('last_evaluated_at')->nullable();

            $table->timestamps();

            // One trust score per owner, or per owner+placement
            $table->unique(['media_owner_id', 'placement_id']);
        });

        Schema::create('trust_events', function (Blueprint $table) {
            $table->id();
            $table->foreignId('media_owner_id')->constrained('media_owners')->onDelete('cascade');
            $table->foreignId('placement_id')->nullable()->constrained('placements')->onDelete('set null');
            $table->foreignId('booking_item_id')->nullable()->constrained('booking_items')->onDelete('set null');

            // Event details
            $table->enum('event_type', [
                'proof_on_time',
                'proof_late',
                'proof_missed',
                'proof_passed',
                'proof_failed',
                'dispute_raised',
                'dispute_won',
                'dispute_lost',
                'cancellation',
                'instant_book_granted',
                'instant_book_revoked',
                'manual_adjustment'
            ]);

            // Score impact
            $table->decimal('score_before', 4, 2)->nullable();
            $table->decimal('score_after', 4, 2)->nullable();
            $table->decimal('score_delta', 4, 2)->nullable();

            $table->text('notes')->nullable();
            $table->foreignId('triggered_by')->nullable()->constrained('users')->onDelete('set null');

            $table->timestamps();
        });
    }

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