<?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::table('tasks', function (Blueprint $table) {
            if (!Schema::hasColumn('tasks', 'visibility')) {
                $table->enum('visibility', ['public', 'department', 'assigned'])->default('public')->after('progress_percent');
            }
            if (!Schema::hasColumn('tasks', 'tags')) {
                $table->text('tags')->nullable()->after('visibility');
            }
        });

        Schema::create('task_watchers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('task_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->timestamps();
            $table->unique(['task_id', 'user_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('task_watchers');
        Schema::table('tasks', function (Blueprint $table) {
            $table->dropColumn(['visibility', 'tags']);
        });
    }
};
