<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('tax_configs', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('code')->unique();
            $table->decimal('rate', 5, 2)->default(0);
            $table->boolean('is_active')->default(true);
            $table->boolean('is_default')->default(false);
            $table->string('type')->default('vat'); // vat, service, excise, other
            $table->text('description')->nullable();
            $table->timestamps();
        });

        Schema::create('etims_configs', function (Blueprint $table) {
            $table->id();
            $table->string('company_name');
            $table->string('company_npwp')->nullable();
            $table->string('company_nik')->nullable();
            $table->string('company_address')->nullable();
            $table->string('company_phone')->nullable();
            $table->string('company_email')->nullable();
            $table->string('branches_id')->nullable();
            $table->string('api_url')->nullable();
            $table->string('api_key')->nullable();
            $table->string('api_secret')->nullable();
            $table->string('client_id')->nullable();
            $table->string('client_secret')->nullable();
            $table->string('machine_id')->default('POS-001');
            $table->string('register_id')->default('REG-001');
            $table->string('serial_number')->nullable();
            $table->boolean('is_production')->default(false);
            $table->boolean('is_active')->default(false);
            $table->text('notes')->nullable();
            $table->timestamps();
        });

        Schema::create('etims_invoices', function (Blueprint $table) {
            $table->id();
            $table->foreignId('order_id')->constrained()->onDelete('cascade');
            $table->string('invoice_number')->unique();
            $table->string('etims_invoice_number')->nullable();
            $table->string('etims_status')->nullable(); // pending, sent, success, failed
            $table->string('status_code')->nullable();
            $table->text('status_message')->nullable();
            $table->timestamp('sent_at')->nullable();
            $table->timestamp('responded_at')->nullable();
            $table->text('request_payload')->nullable();
            $table->text('response_payload')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('etims_invoices');
        Schema::dropIfExists('etims_configs');
        Schema::dropIfExists('tax_configs');
    }
};
