<?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('resources', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('agency_id');
            $table->unsignedBigInteger('supplier_id')->nullable();
            $table->unsignedBigInteger('user_id')->nullable();
            
            $table->string('type', 20); // guide, vehicle, captain
            $table->string('source_type', 20); // internal, external
            $table->string('name');
            $table->string('email')->nullable();
            $table->string('phone', 30)->nullable();
            $table->string('identity_number', 50)->nullable(); // TC, Passport etc
            
            // Vehicle specific columns
            $table->string('vehicle_brand', 50)->nullable();
            $table->string('vehicle_model', 50)->nullable();
            $table->string('vehicle_plate', 30)->nullable();
            $table->integer('vehicle_year')->nullable();
            $table->integer('vehicle_km')->nullable();
            $table->integer('vehicle_capacity')->nullable();
            $table->decimal('vehicle_fuel_consumption', 6, 2)->nullable(); // 1 litre ile gidilen km (km/lt)
            $table->string('vehicle_registration_document')->nullable(); // araç ruhsatı (görsel/pdf) dosya yolu
            
            // Financial columns
            $table->decimal('cost_daily', 10, 2)->default(0);
            $table->decimal('cost_per_km', 10, 2)->default(0);
            
            // License/language details
            $table->string('license_number', 100)->nullable(); // Guide license or driver's license class
            $table->json('languages')->nullable(); // languages spoken for guide

            // Guide specific professional details
            $table->string('guide_registration_no', 100)->nullable(); // Rehber ruhsat (kokart) numarası
            $table->string('guide_chamber', 150)->nullable(); // Bağlı olduğu rehberler odası (ör: İstanbul Rehberler Odası)
            $table->text('guide_service_info')->nullable(); // Hizmet bilgisi / uzmanlık alanı

            $table->boolean('is_active')->default(true);
            $table->string('public_token', 64)->nullable()->unique(); // rehber/şoför için public takvim linki token'ı
            $table->json('extra_details')->nullable();
            
            // Audit fields
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->timestamps();
            $table->softDeletes();

            // Foreign keys
            $table->foreign('agency_id')->references('id')->on('agencies')->onDelete('cascade');
            $table->foreign('supplier_id')->references('id')->on('suppliers')->onDelete('set null');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('set null');
            $table->foreign('created_by')->references('id')->on('users')->onDelete('set null');
            $table->foreign('updated_by')->references('id')->on('users')->onDelete('set null');

            // Indexes
            $table->index(['agency_id', 'type', 'is_active']);
            $table->index(['agency_id', 'source_type']);
            $table->index(['user_id']);
        });

        Schema::create('resource_schedules', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('resource_id');
            $table->unsignedBigInteger('agency_id');
            
            $table->date('start_date');
            $table->date('end_date');
            $table->time('start_time')->nullable();
            $table->time('end_time')->nullable();
            
            $table->string('schedule_type', 30); // block, tour_assignment, maintenance, leave
            $table->string('title');
            $table->text('description')->nullable();
            
            $table->string('reference_model_type')->nullable(); // DayTourDate or CulturalTourDate model class name
            $table->unsignedBigInteger('reference_model_id')->nullable();
            $table->boolean('is_approved')->default(true);
            
            // Audit fields
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->timestamps();
            $table->softDeletes();

            // Foreign keys
            $table->foreign('resource_id')->references('id')->on('resources')->onDelete('cascade');
            $table->foreign('agency_id')->references('id')->on('agencies')->onDelete('cascade');
            $table->foreign('created_by')->references('id')->on('users')->onDelete('set null');
            $table->foreign('updated_by')->references('id')->on('users')->onDelete('set null');

            // Indexes
            $table->index(['resource_id', 'start_date', 'end_date']);
            $table->index(['agency_id', 'start_date', 'end_date']);
            $table->index(['reference_model_type', 'reference_model_id']);
        });
    }

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