<?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
    {
        // 1. Roles & Permissions
        Schema::create('roles', function (Blueprint $table) {
            $table->id();
            $table->string('name'); // Admin, Manager, User, AI_Agent
            $table->json('permissions')->nullable();
            $table->timestamps();
        });

        Schema::table('users', function (Blueprint $table) {
            $table->unsignedBigInteger('role_id')->nullable()->after('password');
            $table->boolean('is_ai_bot')->default(false)->after('role_id');
            $table->string('status')->default('active')->after('is_ai_bot');
        });

        // 2. CRM System
        Schema::create('companies', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('industry')->nullable();
            $table->string('website')->nullable();
            $table->timestamps();
        });

        Schema::create('contacts', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('company_id')->nullable();
            $table->string('first_name');
            $table->string('last_name')->nullable();
            $table->string('email')->unique();
            $table->string('phone')->nullable();
            $table->timestamps();
        });

        Schema::create('deals', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('contact_id')->nullable();
            $table->string('title');
            $table->decimal('value', 15, 2)->default(0);
            $table->string('stage')->default('lead'); // lead, negotiation, closed_won, closed_lost
            $table->timestamps();
        });

        // 3. Communication System
        Schema::create('conversations', function (Blueprint $table) {
            $table->id();
            $table->string('type')->default('personal'); // personal, group, channel
            $table->string('name')->nullable(); // For groups/channels
            $table->timestamps();
        });

        Schema::create('conversation_participants', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('conversation_id');
            $table->unsignedBigInteger('user_id');
            $table->timestamps();
        });

        Schema::create('messages', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('conversation_id');
            $table->unsignedBigInteger('sender_id');
            $table->text('content');
            $table->string('message_type')->default('text'); // text, system, ai
            $table->json('metadata')->nullable();
            $table->timestamps();
        });

        // 4. Task Management System
        Schema::create('tasks', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('created_by');
            $table->unsignedBigInteger('assigned_to')->nullable();
            $table->unsignedBigInteger('parent_task_id')->nullable(); // For dependencies
            $table->string('title');
            $table->text('description')->nullable();
            $table->string('priority')->default('medium'); // low, medium, high, urgent
            $table->string('status')->default('todo'); // todo, in_progress, review, done
            $table->dateTime('deadline')->nullable();
            $table->nullableMorphs('linked_entity'); // Links to Deals, Contacts, etc.
            $table->timestamps();
        });

        // 5. AI Audit & Automation
        Schema::create('ai_audit_logs', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id')->nullable();
            $table->string('action_type');
            $table->text('reasoning')->nullable();
            $table->json('input_payload');
            $table->json('output_payload');
            $table->float('confidence')->default(0);
            $table->string('mode')->default('assistant'); // assistant, manager, admin
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('ai_audit_logs');
        Schema::dropIfExists('tasks');
        Schema::dropIfExists('messages');
        Schema::dropIfExists('conversation_participants');
        Schema::dropIfExists('conversations');
        Schema::dropIfExists('deals');
        Schema::dropIfExists('contacts');
        Schema::dropIfExists('companies');
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn(['role_id', 'is_ai_bot', 'status']);
        });
        Schema::dropIfExists('roles');
    }
};
