<?php

use App\Repos\Order\OrderRepo;
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('loan_orders', function (Blueprint $table) {
            $table->id();
            $table->string('unique_id')->nullable()->unique();

            $table->foreignId('loan_type')->constrained()->restrictOnDelete();

            $table->string('region', 2)->index();
            $table->foreignId('branch_id')->constrained()->restrictOnDelete();

            $table->string('customer_name')->index();
            $table->string('customer_surname')->index();
            $table->string('customer_patronic_name')->nullable();

            $table->string('passport_address');
            $table->string('real_address');

            $table->string('passport_serie')->index();
            $table->integer('passport_id')->index();
            $table->date('passport_given_at');
            $table->string('passport_given_by');

            $table->string('born_place');
            $table->date('born_at');

            $table->string('email')->nullable()->index();
            $table->string('phone')->index();
            $table->string('phone_additional')->nullable();
            $table->string('phone_home')->nullable();

            $table->string('work_region')->nullable()->index();
            $table->string('work_province')->nullable();
            $table->string('work_company')->nullable();
            $table->string('work_company_accountant_number')->nullable();
            $table->date('work_started_at')->nullable();
            $table->string('work_salary')->nullable();
            $table->string('work_position')->nullable();

            $table->string('education');
            $table->string('marriage_status');

            $table->text('passport_one');
            $table->text('passport_two');
            $table->text('passport_three');
            $table->text('passport_four');

            $table->foreignId('filled_by')->nullable()->constrained('users')->restrictOnDelete();
            $table->foreignId('user_id')->nullable()->constrained('users')->restrictOnDelete();

            $table->string('status')->nullable()->default(OrderRepo::defaultStatus())->index();
            $table->string('status_reason')->nullable()->default('');

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

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

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