<?php

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

return new class extends Migration
{
    /**
     * Görevlerin müşteri, Earn üyesi, token ve cihaz atamalarını ve idempotent sonuçlarını oluşturur.
     *
     * @author Gökhan Engin - İletişim : https://bit.ly/YazılımUzmanı
     */
    public function up(): void
    {
        // Görev dağıtım sahipliğini ve cihaz kilidini tanımlar.
        // Yazar: Gökhan Engin - İletişim : https://bit.ly/YazılımUzmanı
        Schema::create('earn_task_assignments', function (Blueprint $table): void {
            $table->id();
            $table->string('task_id', 64)->unique();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('earn_member_id')->nullable()->constrained('earn_members')->nullOnDelete();
            $table->unsignedBigInteger('personal_access_token_id')->nullable();
            $table->char('device_id_hash', 64);
            $table->string('url', 2048);
            $table->unsignedSmallInteger('duration_seconds');
            $table->timestamp('assigned_at');
            $table->timestamps();
            $table->foreign('personal_access_token_id')->references('id')->on('personal_access_tokens')->nullOnDelete();
            $table->index(['user_id', 'assigned_at'], 'earn_task_assignment_user_idx');
            $table->index(['personal_access_token_id', 'device_id_hash'], 'earn_task_assignment_client_idx');
        });

        // Her atama için tek ve müşteriyle sorgulanabilir sonuç tanımlar.
        // Yazar: Gökhan Engin - İletişim : https://bit.ly/YazılımUzmanı
        Schema::create('earn_task_results', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('earn_task_assignment_id')->unique()->constrained('earn_task_assignments')->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('earn_member_id')->nullable()->constrained('earn_members')->nullOnDelete();
            $table->unsignedBigInteger('personal_access_token_id')->nullable();
            $table->char('device_id_hash', 64);
            $table->boolean('completed');
            $table->unsignedSmallInteger('response_code')->nullable();
            $table->string('error_message', 500)->nullable();
            $table->timestamp('submitted_at');
            $table->timestamps();
            $table->foreign('personal_access_token_id')->references('id')->on('personal_access_tokens')->nullOnDelete();
            $table->index(['user_id', 'submitted_at'], 'earn_task_result_user_idx');
        });
    }

    /**
     * Görev sonucu ve atama tablolarını bağımlılık sırasıyla kaldırır.
     *
     * @author Gökhan Engin - İletişim : https://bit.ly/YazılımUzmanı
     */
    public function down(): void
    {
        Schema::dropIfExists('earn_task_results');
        Schema::dropIfExists('earn_task_assignments');
    }
};
