<?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::table('users', function (Blueprint $table) {
            $table->foreignId('company_id')->nullable()->after('id')->constrained()->onDelete('cascade');
            $table->foreignId('project_id')->nullable()->after('company_id')->constrained()->onDelete('set null');
            $table->string('invitation_token')->nullable()->unique()->after('remember_token');
            $table->timestamp('invitation_accepted_at')->nullable()->after('invitation_token');
            
            // Actualizar campo company existente
            $table->dropColumn('company');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropForeign(['company_id']);
            $table->dropForeign(['project_id']);
            $table->dropColumn(['company_id', 'project_id', 'invitation_token', 'invitation_accepted_at']);
            $table->string('company')->nullable();
        });
    }
};
