<?php

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

return new class extends Migration
{
    /**
     * 소프트 삭제 테이블에 deleted_by 컬럼 추가
     *
     * deleted_at이 있지만 deleted_by가 없는 테이블 30개에 추가
     */
    public function up(): void
    {
        $tables = [
            'board_comments',
            'bom_templates',
            'classifications',
            'common_codes',
            'department_user',
            'lot_sales',
            'lots',
            'main_requests',
            'material_inspection_items',
            'material_inspections',
            'material_receipts',
            'materials',
            'model_versions',
            'models',
            'order_item_components',
            'order_items',
            'orders',
            'payments',
            'permission_overrides',
            'plans',
            'posts',
            'price_histories',
            'product_components',
            'products',
            'roles',
            'subscriptions',
            'tenants',
            'user_roles',
            'user_tenants',
            'users',
        ];

        foreach ($tables as $tableName) {
            if (! Schema::hasTable($tableName)) {
                continue;
            }

            Schema::table($tableName, function (Blueprint $table) use ($tableName) {
                if (! Schema::hasColumn($tableName, 'deleted_by')) {
                    $table->foreignId('deleted_by')
                        ->nullable()
                        ->after('deleted_at')
                        ->comment('삭제자 사용자 ID');
                }
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        $tables = [
            'board_comments',
            'bom_templates',
            'classifications',
            'common_codes',
            'department_user',
            'lot_sales',
            'lots',
            'main_requests',
            'material_inspection_items',
            'material_inspections',
            'material_receipts',
            'materials',
            'model_versions',
            'models',
            'order_item_components',
            'order_items',
            'orders',
            'payments',
            'permission_overrides',
            'plans',
            'posts',
            'price_histories',
            'product_components',
            'products',
            'roles',
            'subscriptions',
            'tenants',
            'user_roles',
            'user_tenants',
            'users',
        ];

        foreach ($tables as $tableName) {
            if (! Schema::hasTable($tableName)) {
                continue;
            }

            Schema::table($tableName, function (Blueprint $table) use ($tableName) {
                if (Schema::hasColumn($tableName, 'deleted_by')) {
                    $table->dropColumn('deleted_by');
                }
            });
        }
    }
};
