<?php

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

return new class extends Migration
{
    public function up(): void
    {
        $tenants = DB::table('tenants')->whereNull('deleted_at')->pluck('id');

        foreach ($tenants as $tenantId) {
            $exists = DB::table('approval_forms')
                ->where('tenant_id', $tenantId)
                ->where('code', 'quotation')
                ->exists();

            if (! $exists) {
                DB::table('approval_forms')->insert([
                    'tenant_id' => $tenantId,
                    'name' => '견적서',
                    'code' => 'quotation',
                    'category' => 'expense',
                    'template' => '[]',
                    'body_template' => '',
                    'is_active' => true,
                    'created_at' => now(),
                    'updated_at' => now(),
                ]);
            }
        }
    }

    public function down(): void
    {
        DB::table('approval_forms')->where('code', 'quotation')->delete();
    }
};
