<?php

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

return new class extends Migration
{
    public function up(): void
    {
        if (!Schema::hasTable('tax_settings')) {
            Schema::create('tax_settings', function (Blueprint $table) {
                $table->id();
                $table->string('name')->default('PPN');
                $table->decimal('rate', 5, 2)->default(11.00);
                $table->boolean('is_active')->default(false)->index();
                $table->string('npwp')->nullable();
                $table->string('pkp_name')->nullable();
                $table->text('pkp_address')->nullable();
                $table->text('notes')->nullable();
                $table->timestamps();
            });
        }

        if (Schema::hasTable('tax_settings') && DB::table('tax_settings')->count() === 0) {
            DB::table('tax_settings')->insert([
                'name' => 'PPN',
                'rate' => 11.00,
                'is_active' => false,
                'notes' => 'Aktifkan jika invoice ingin otomatis menggunakan PPN.',
                'created_at' => now(),
                'updated_at' => now(),
            ]);
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('tax_settings');
    }
};
