<?php

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

return new class extends Migration
{
    /**
     * codebridge DB로 이관 완료된 58개 테이블을 sam DB에서 삭제
     *
     * 복원: ~/backups/sam_codebridge_tables_20260309.sql
     */
    private array $tables = [
        // Admin (9)
        'admin_api_flows',
        'admin_api_flow_runs',
        'admin_pm_daily_logs',
        'admin_pm_daily_log_entries',
        'admin_pm_issues',
        'admin_pm_projects',
        'admin_pm_tasks',
        'admin_roadmap_milestones',
        'admin_roadmap_plans',

        // DevTools (5)
        'admin_api_bookmarks',
        'admin_api_deprecations',
        'admin_api_environments',
        'admin_api_histories',
        'admin_api_templates',

        // Sales (17)
        'sales_partners',
        'sales_managers',
        'sales_manager_documents',
        'sales_commissions',
        'sales_commission_details',
        'sales_consultations',
        'sales_contract_products',
        'sales_products',
        'sales_product_categories',
        'sales_prospects',
        'sales_prospect_consultations',
        'sales_prospect_products',
        'sales_prospect_scenarios',
        'sales_records',
        'sales_scenario_checklists',
        'sales_tenant_managements',
        'tenant_prospects',

        // Finance (9)
        'condolence_expenses',
        'consulting_fees',
        'corporate_cards',
        'corporate_card_prepayments',
        'customer_settlements',
        'daily_fund_memos',
        'daily_fund_transactions',
        'incomes',
        'vat_records',

        // ESign (2)
        'esign_field_templates',
        'esign_field_template_items',

        // Equipment (6)
        'equipments',
        'equipment_process',
        'equipment_inspections',
        'equipment_inspection_details',
        'equipment_inspection_templates',
        'equipment_repairs',

        // HR (1)
        'business_income_payments',

        // System (1)
        'ai_configs',

        // 기타 (8)
        'biz_cert',
        'cm_songs',
        'construction_site_photos',
        'construction_site_photo_rows',
        'admin_meeting_logs',
        'meeting_minutes',
        'meeting_minute_segments',
        'interview_knowledge',
    ];

    public function up(): void
    {
        DB::statement('SET FOREIGN_KEY_CHECKS = 0');

        foreach ($this->tables as $table) {
            Schema::dropIfExists($table);
        }

        DB::statement('SET FOREIGN_KEY_CHECKS = 1');
    }

    public function down(): void
    {
        // 복원은 백업 파일로 수행
        // mysql -u codebridge -p sam < ~/backups/sam_codebridge_tables_20260309.sql
    }
};
