<?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('disputes', function (Blueprint $table) {
            $table->id();
            $table->string('reference')->unique(); // e.g. DSP-000142
            $table->foreignId('booking_id')->constrained('bookings')->onDelete('cascade');
            $table->foreignId('booking_item_id')->constrained('booking_items')->onDelete('cascade');
            $table->foreignId('raised_by')->constrained('users')->onDelete('cascade');
            $table->foreignId('against_user_id')->nullable()->constrained('users')->onDelete('set null');
            $table->foreignId('media_owner_id')->nullable()->constrained('media_owners')->onDelete('set null');

            // Dispute details
            $table->enum('reason', [
                'proof_missing',
                'proof_late',
                'wrong_creative',
                'obstructed',
                'poor_visibility',
                'placement_mismatch',
                'no_deployment',
                'other'
            ]);

            $table->string('reason_label')->nullable();
            $table->text('description');

            // Status
            $table->enum('status', [
                'open',
                'under_review',
                'awaiting_response',
                'resolved_advertiser',
                'resolved_owner',
                'resolved_split',
                'closed'
            ])->default('open');

            // Resolution
            $table->text('resolution_notes')->nullable();
            $table->enum('resolution_outcome', [
                'full_refund',
                'partial_refund',
                'payout_released',
                'no_action',
                'penalty_applied'
            ])->nullable();

            $table->decimal('refund_amount', 14, 2)->default(0.00);
            $table->decimal('penalty_amount', 14, 2)->default(0.00);

            // Admin handling
            $table->foreignId('assigned_to')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamp('resolved_at')->nullable();
            $table->foreignId('resolved_by')->nullable()->constrained('users')->onDelete('set null');

            // Payout impact
            $table->boolean('payout_held')->default(true);
            $table->timestamp('payout_held_at')->nullable();
            $table->timestamp('payout_released_at')->nullable();

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

        Schema::create('dispute_messages', function (Blueprint $table) {
            $table->id();
            $table->foreignId('dispute_id')->constrained('disputes')->onDelete('cascade');
            $table->foreignId('sent_by')->constrained('users')->onDelete('cascade');

            // Message
            $table->text('message');
            $table->enum('sender_role', ['advertiser', 'media_owner', 'admin', 'system']);
            $table->boolean('is_internal')->default(false); // admin only notes

            // Attachments
            $table->json('attachments')->nullable();

            $table->timestamp('read_at')->nullable();
            $table->timestamps();
        });
    }

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