<?php

use App\Enums\EntityStatus;
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('customers', function (Blueprint $table) {
            $table->id();
            $table->string('name')->nullable();
            $table->string('email')->nullable();
            $table->enum('gender', ['M', 'F','other'])->nullable();
            $table->string('ci')->nullable();
            $table->string('phone')->unique();
            $table->string('cod_phone')->nullable();
            $table->date('date_of_birthday')->nullable();
            $table->string('workplace')->nullable();
            $table->string('profile_image')->nullable();
            $table->string('thread')->nullable();

            $table->unsignedBigInteger('conversation_id')->nullable();
            $table->foreign('conversation_id')->references('id')->on('conversations');

            $table->unsignedBigInteger('source_id')->nullable();
            $table->foreign('source_id')->references('id')->on('sources');

            $table->unsignedBigInteger('contact_method_id')->nullable();
            $table->foreign('contact_method_id')->references('id')->on('contact_methods');

            $table->unsignedBigInteger('customer_references_id')->nullable();
            $table->foreign('customer_references_id')->references('id')->on('customer_references');

            $table->enum('entity_status',[EntityStatus::ACTIVE->value, EntityStatus::INACTIVE->value])->default(EntityStatus::ACTIVE->value);
            $table->timestamps();

            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->unsignedBigInteger('deleted_by')->nullable();
            $table->foreign('created_by')->references('id')->on('users');
            $table->foreign('updated_by')->references('id')->on('users');
            $table->foreign('deleted_by')->references('id')->on('users');
            $table->softDeletes();
        });

        Schema::table('customer_references', function (Blueprint $table) {
            $table->unsignedBigInteger('referred_by_customer_id')->nullable();
            $table->foreign('referred_by_customer_id')->references('id')->on('customers')->onDelete('cascade');
        });
    }

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