<?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::create('tasks', function (Blueprint $table) {
            $table->id();
            $table->foreignId('lead_id')->nullable()->constrained('leads')->cascadeOnDelete();
            $table->foreignId('project_id')->nullable()->constrained('projects')->cascadeOnDelete();
            $table->foreignId('project_ticket_id')->nullable()->constrained('project_tickets')->cascadeOnDelete();
            $table->foreignId('created_by')->constrained('users')->cascadeOnDelete();
            $table->foreignId('assigned_to')->constrained('users')->cascadeOnDelete();
            $table->string('name');
            $table->text('description')->nullable();
            $table->date('start_date')->nullable();
            $table->date('due_date')->nullable();
            $table->enum('priority', ['low', 'medium', 'high'])->default('low');
            $table->enum('type', ['one_time', 'recurring'])->default('one_time');
            $table->enum('interval', ['daily', 'weekly', 'fortnightly', 'monthly', 'quarterly', 'half_yearly', 'yearly'])->nullable();
            $table->unsignedTinyInteger('is_completed')->default(0);
            $table->dateTime('completed_at')->nullable();
            $table->unsignedTinyInteger('has_recurred')->default(0);
            $table->unsignedTinyInteger('stop_recurrence')->default(0);
            $table->dateTime('stop_recurrence_time')->nullable();
            $table->foreignId('stop_recurrence_by')->nullable()->constrained('users')->cascadeOnDelete();
            $table->unsignedTinyInteger('is_deleted')->default(0);
            $table->timestamps();
            $table->softDeletes();
        });
    }

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