<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    public function up(): void
    {
        Schema::create('tasks', function (Blueprint $table) {
            $table->id();

            $table->foreignId('department_id')->constrained('departments')->cascadeOnDelete();

            $table->string('title', 150);
            $table->text('description')->nullable();

            $table->enum('priority', ['low','medium','high','critical'])->default('medium')->index();
            $table->enum('status', ['backlog','todo','in_progress','blocked','done','cancelled'])->default('todo')->index();

            $table->date('start_date')->nullable()->index();
            $table->date('due_date')->nullable()->index();
            $table->unsignedInteger('estimate_minutes')->nullable();
            $table->unsignedInteger('actual_minutes')->nullable();
            $table->tinyInteger('complexity')->nullable(); // 1-5

            $table->foreignId('created_by')->constrained('users')->cascadeOnDelete();

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

            $table->index(['status','due_date']);
            $table->index(['department_id','status']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('tasks');
    }
};
