<?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('leases', function (Blueprint $table) {

            $table->id();

            $table->string('lease_no')->unique();

            $table->foreignId('device_id')
                ->constrained()
                ->cascadeOnDelete();

            $table->enum('lease_type', [
                'cash',
                'bill'
            ]);

            // CASH = customer_name
            // BILL = customer_id
            $table->foreignId('customer_id')
                ->nullable();

            $table->string('customer_name')
                ->nullable();

            $table->string('sub_t1_no');

            $table->string('truck_no');

            $table->string('trailer_no')
                ->nullable();

            $table->string('driver_name');

            $table->string('driver_phone');

            $table->string('passport_or_license');

            $table->enum('cargo_type', [
                'CONTAINERIZED',
                'IT',
                'DRY CARGO',
                'FUEL TANKER',
                'BOX BODY',
            ]);

            $table->string('agent_name');

            $table->foreignId('destination_border_id');

            $table->foreignId('tag_point_id');

            $table->decimal('amount', 12, 2);

            $table->foreignId('sales_user_id');

            $table->enum('status', [
                'active',
                'audited',
                'arrived_border',
                'returned',
                'completed'
            ])->default('active');

            // Audit fields
            $table->timestamp('audited_at')
                ->nullable();

            $table->foreignId('audited_by')
                ->nullable();

            $table->text('audit_notes')
                ->nullable();

            // Tracking
            $table->timestamp('leased_at');

            $table->timestamp('returned_at')
                ->nullable();

            $table->foreignId('created_by');

            $table->foreignId('updated_by');

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

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