<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * admin_pm_issues 테이블에 팀/담당자/고객사 필드 추가
     * - 하이브리드 방식: FK + 문자열 필드 (연동 선택 가능)
     */
    public function up(): void
    {
        Schema::table('admin_pm_issues', function (Blueprint $table) {
            // 부서 (하이브리드: FK 또는 문자열)
            $table->foreignId('department_id')
                ->nullable()
                ->after('is_urgent')
                ->constrained('departments')
                ->nullOnDelete()
                ->comment('부서 FK (연동 시)');
            $table->string('team', 100)
                ->nullable()
                ->after('department_id')
                ->comment('팀/부서명 (직접 입력)');

            // 담당자 (하이브리드: FK 또는 문자열)
            $table->foreignId('assignee_id')
                ->nullable()
                ->after('team')
                ->constrained('users')
                ->nullOnDelete()
                ->comment('담당자 FK (연동 시)');
            $table->string('assignee_name', 100)
                ->nullable()
                ->after('assignee_id')
                ->comment('담당자명 (직접 입력)');

            // 고객사
            $table->string('client', 100)
                ->nullable()
                ->after('assignee_name')
                ->comment('고객사명');

            // 인덱스
            $table->index('department_id', 'idx_admin_pm_issues_department');
            $table->index('assignee_id', 'idx_admin_pm_issues_assignee');
            $table->index('client', 'idx_admin_pm_issues_client');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('admin_pm_issues', function (Blueprint $table) {
            // 인덱스 삭제
            $table->dropIndex('idx_admin_pm_issues_department');
            $table->dropIndex('idx_admin_pm_issues_assignee');
            $table->dropIndex('idx_admin_pm_issues_client');

            // FK 삭제
            $table->dropForeign(['department_id']);
            $table->dropForeign(['assignee_id']);

            // 컬럼 삭제
            $table->dropColumn([
                'department_id',
                'team',
                'assignee_id',
                'assignee_name',
                'client',
            ]);
        });
    }
};
