<?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::table('vehicles', function (Blueprint $table) {
            if (! Schema::hasColumn('vehicles', 'branch_id')) {
                $table->unsignedBigInteger('branch_id')->nullable()->after('id');
            }
            $table->string('driver_photo', 20000)->nullable()->after('driver_phone');
            $table->string('driver_national_id')->nullable()->after('driver_photo');
            $table->string('driver_license')->nullable()->after('driver_national_id');
            $table->text('guarantee_form')->nullable()->after('driver_license'); // فورم ضمانت (base64 doc)
            $table->string('car_photo', 20000)->nullable()->after('guarantee_form');
            $table->string('shift')->nullable()->after('car_photo'); // morning / afternoon / both
            $table->string('area')->nullable()->after('shift');
            $table->text('notes')->nullable()->after('area');
        });

        Schema::create('transport_student', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('vehicle_id');
            $table->unsignedBigInteger('student_id');
            $table->string('shift')->nullable();        // morning / afternoon
            $table->string('pickup_time')->nullable();
            $table->string('drop_time')->nullable();
            $table->string('stop')->nullable();          // pickup point / area
            $table->string('side')->nullable();          // which direction / route side
            $table->timestamps();
            $table->index(['vehicle_id', 'shift']);
            $table->unique(['vehicle_id', 'student_id', 'shift']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('transport_student');
        Schema::table('vehicles', function (Blueprint $table) {
            $table->dropColumn(['driver_photo', 'driver_national_id', 'driver_license', 'guarantee_form', 'car_photo', 'shift', 'area', 'notes']);
        });
    }
};
