<?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('loan_orders', function (Blueprint $table) {
            // Unique ID
            $table->id();
            $table->string('unique_id')->nullable()->unique();

            // web, app
            $table->string('source')->default('web')->index()->nullable();

            // User
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();

            // Loan Type
            $table->foreignId('loan_type')->constrained('loan_types')->restrictOnDelete();

            // Region & Branch
            $table->string('region', 2);
            $table->foreignId('branch_id')->constrained('branches')->restrictOnDelete();

            // Customer name,surname,patronic name
            $table->string('customer_name')->index();
            $table->string('customer_surname')->index();
            $table->string('customer_patronic_name')->nullable();

            // Passport address, real address
            $table->string('passport_address');
            $table->string('real_address');

            // Passport data
            $table->string('passport_serie')->index();
            $table->string('passport_id')->index();
            $table->date('passport_given_at');
            $table->string('passport_given_by');
            $table->string('born_place');
            $table->date('born_at');

            // Email, phone, phone additional, phone home
            $table->string('email')->nullable();

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

            $table->string('work_region')->nullable()->index();
            $table->foreignId('work_province_id')->nullable()->constrained('provinces')->restrictOnDelete();

            $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')->index();
            $table->string('marriage_status')->index();

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

            $table->string('loan_amount')->nullable()->index();

            $table->string('card_number')->nullable();
            $table->string('card_name')->nullable();
            $table->string('card_month')->nullable();
            $table->string('card_year')->nullable();

            // Guarantor one begin
            $table->string('guarantor_name')->nullable();
            $table->string('guarantor_surname')->nullable();
            $table->string('guarantor_patronic_name')->nullable();
            $table->string('guarantor_passport_serie')->nullable();
            $table->string('guarantor_passport_id')->nullable();

            $table->string('guarantor_card_number')->nullable();
            $table->string('guarantor_card_name')->nullable();
            $table->string('guarantor_card_month')->nullable();
            $table->string('guarantor_card_year')->nullable();

            $table->string('guarantor_note')->nullable();
            // Guarantor one end

            // Guarantor two begin
            $table->string('guarantor_2_name')->nullable();
            $table->string('guarantor_2_surname')->nullable();
            $table->string('guarantor_2_patronic_name')->nullable();

            $table->string('guarantor_2_passport_serie')->nullable();
            $table->string('guarantor_2_passport_id')->nullable();

            $table->string('guarantor_2_card_number')->nullable();
            $table->string('guarantor_2_card_name')->nullable();
            $table->string('guarantor_2_card_month')->nullable();
            $table->string('guarantor_2_card_year')->nullable();

            $table->string('guarantor_2_note')->nullable();
            // Guarantor two end

            // Loan card begin
            $table->string('loan_card_number')->nullable();
            $table->string('loan_card_name')->nullable();
            $table->string('loan_card_month')->nullable();
            $table->string('loan_card_year')->nullable();

            $table->foreignId('loan_order_required_doc_id')->nullable()->constrained('loan_order_required_docs')->restrictOnDelete();
            // Loan card end

            $table->string('status')->nullable();
            $table->string('satisfiable')->nullable();

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

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

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