<?php

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

return new class extends Migration
{
    public function up()
    {
        // جدول الوكلاء
        Schema::create('agents', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('phone')->unique();
            $table->string('email')->unique();
            $table->string('governorate');
            $table->string('city');
            $table->text('address');
            $table->enum('type', ['new', 'premium'])->default('new');
            $table->decimal('rating', 3, 2)->default(0);
            $table->integer('total_transactions')->default(0);
            $table->boolean('is_active')->default(true);
            $table->string('profile_image')->nullable();
            $table->text('description')->nullable();
            $table->json('working_hours')->nullable();
            $table->json('payment_methods')->nullable();
            $table->timestamps();
        });

        // جدول معاملات الوكلاء
        Schema::create('agent_transactions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('agent_id')->constrained()->onDelete('cascade');
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->decimal('amount', 10, 2);
            $table->integer('gems_amount');
            $table->enum('status', ['pending', 'completed', 'cancelled'])->default('pending');
            $table->string('payment_method');
            $table->string('transaction_id')->unique();
            $table->text('notes')->nullable();
            $table->timestamps();
        });

        // جدول تقييمات الوكلاء
        Schema::create('agent_reviews', function (Blueprint $table) {
            $table->id();
            $table->foreignId('agent_id')->constrained()->onDelete('cascade');
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('transaction_id')->constrained('agent_transactions')->onDelete('cascade');
            $table->integer('rating'); // 1-5
            $table->text('comment')->nullable();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('agent_reviews');
        Schema::dropIfExists('agent_transactions');
        Schema::dropIfExists('agents');
    }
};
