<?php

use App\Enums\EntityStatus;
use App\Enums\StagesOpportunity;
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('opportunities', function (Blueprint $table) {
            $table->id();
            $table->date('start_date');
            $table->date('end_date')->nullable();
            $table->longText('description')->nullable();
            $table->unsignedBigInteger('stage_id')->default(StagesOpportunity::LEAD->value());
            $table->foreign('stage_id')->references('id')->on('stages');
            $table->unsignedBigInteger('customer_id');
            $table->foreign('customer_id')->references('id')->on('customers');
            $table->unsignedBigInteger('user_id')->nullable();
            $table->foreign('user_id')->references('id')->on('users'); //vendedor
            $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');

            //todo: ver si relacionarlo con una sucursal, ya queel usuario ya esta relacionado a un sucrusal, aunque para mayor facilidad podriamos ponerlos

            $table->unsignedBigInteger('property_id')->nullable();
            $table->foreign('property_id')->references('id')->on('properties');

            $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();
        });
    }

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