<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * 프로젝트 관리 기능을 위한 테이블 생성 (MNG 전용)
     * - admin_pm_projects: 프로젝트 정의
     * - admin_pm_tasks: 작업(할일) 관리
     * - admin_pm_issues: 이슈 관리
     */
    public function up(): void
    {
        // admin_pm_projects: 프로젝트 테이블
        Schema::create('admin_pm_projects', function (Blueprint $table) {
            $table->id()->comment('ID');
            $table->string('name', 100)->comment('프로젝트 이름');
            $table->text('description')->nullable()->comment('설명');
            $table->string('status', 20)->default('active')->comment('상태: active, completed, on_hold');
            $table->date('start_date')->nullable()->comment('시작일');
            $table->date('end_date')->nullable()->comment('종료일');
            $table->foreignId('created_by')->nullable()->comment('생성자 ID');
            $table->foreignId('updated_by')->nullable()->comment('수정자 ID');
            $table->foreignId('deleted_by')->nullable()->comment('삭제자 ID');
            $table->timestamps();
            $table->softDeletes();

            // 인덱스
            $table->index('status', 'idx_admin_pm_projects_status');
            $table->index('name', 'idx_admin_pm_projects_name');
        });

        // admin_pm_tasks: 작업(할일) 테이블
        Schema::create('admin_pm_tasks', function (Blueprint $table) {
            $table->id()->comment('ID');
            $table->foreignId('project_id')->comment('프로젝트 ID')
                ->constrained('admin_pm_projects')
                ->onDelete('cascade');
            $table->string('title', 255)->comment('작업 제목');
            $table->text('description')->nullable()->comment('작업 설명');
            $table->string('status', 20)->default('todo')->comment('상태: todo, in_progress, done');
            $table->string('priority', 10)->default('medium')->comment('우선순위: low, medium, high');
            $table->date('due_date')->nullable()->comment('마감일');
            $table->unsignedInteger('sort_order')->default(0)->comment('정렬 순서');
            $table->foreignId('assignee_id')->nullable()->comment('담당자 ID');
            $table->foreignId('created_by')->nullable()->comment('생성자 ID');
            $table->foreignId('updated_by')->nullable()->comment('수정자 ID');
            $table->foreignId('deleted_by')->nullable()->comment('삭제자 ID');
            $table->timestamps();
            $table->softDeletes();

            // 인덱스
            $table->index('project_id', 'idx_admin_pm_tasks_project_id');
            $table->index('status', 'idx_admin_pm_tasks_status');
            $table->index('priority', 'idx_admin_pm_tasks_priority');
            $table->index('due_date', 'idx_admin_pm_tasks_due_date');
            $table->index('sort_order', 'idx_admin_pm_tasks_sort_order');
        });

        // admin_pm_issues: 이슈 테이블
        Schema::create('admin_pm_issues', function (Blueprint $table) {
            $table->id()->comment('ID');
            $table->foreignId('project_id')->comment('프로젝트 ID')
                ->constrained('admin_pm_projects')
                ->onDelete('cascade');
            $table->foreignId('task_id')->nullable()->comment('연결된 작업 ID')
                ->constrained('admin_pm_tasks')
                ->onDelete('set null');
            $table->string('title', 255)->comment('이슈 제목');
            $table->text('description')->nullable()->comment('이슈 설명');
            $table->string('type', 20)->default('bug')->comment('타입: bug, feature, improvement');
            $table->string('status', 20)->default('open')->comment('상태: open, in_progress, resolved, closed');
            $table->foreignId('created_by')->nullable()->comment('생성자 ID');
            $table->foreignId('updated_by')->nullable()->comment('수정자 ID');
            $table->foreignId('deleted_by')->nullable()->comment('삭제자 ID');
            $table->timestamps();
            $table->softDeletes();

            // 인덱스
            $table->index('project_id', 'idx_admin_pm_issues_project_id');
            $table->index('task_id', 'idx_admin_pm_issues_task_id');
            $table->index('type', 'idx_admin_pm_issues_type');
            $table->index('status', 'idx_admin_pm_issues_status');
        });
    }

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