<?php

declare(strict_types=1);

use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Migrations\Migration;

class CreateBarangayDocumentRequestsTable extends Migration
{
    public function up(): void
    {
        Schema::create('barangay_request_types', function (Blueprint $table) {
            $table->id();
            $table->string('hashkey')->unique()->nullable();
            $table->string('name');
            $table->string('code')->unique();
            $table->text('description')->nullable();
            $table->decimal('base_fee', 10, 2)->default(0);
            $table->unsignedInteger('processing_days')->default(1);
            $table->boolean('is_active')->default(true);
            $table->boolean('requires_clearance')->default(false);
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->timestamps();
        });

        Schema::create('barangay_document_requests', function (Blueprint $table) {
            $table->id();
            $table->string('hashkey')->unique()->nullable();
            $table->string('request_no')->unique()->nullable();
            $table->unsignedBigInteger('resident_user_id')->nullable()->index();
            $table->unsignedBigInteger('request_type_id')->index();
            $table->text('purpose')->nullable();
            $table->decimal('fee_amount', 10, 2)->default(0);
            $table->enum('payment_status', ['PENDING', 'PAID', 'REFUNDED', 'WAIVED'])->default('PENDING');
            $table->enum('payment_method', ['CASH', 'GCASH', 'QRPH', 'BANK', 'WAIVED'])->nullable();
            $table->string('payment_ref')->nullable();
            $table->text('qrph_code')->nullable();
            $table->enum('status', ['DRAFT', 'PENDING_PAYMENT', 'PAID', 'PROCESSING', 'READY', 'CLAIMED', 'CANCELLED'])->default('DRAFT');
            $table->unsignedBigInteger('requested_by')->nullable();
            $table->unsignedBigInteger('processed_by')->nullable();
            $table->timestamp('claimed_at')->nullable();
            $table->text('notes')->nullable();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('barangay_request_payments', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('request_id')->index();
            $table->decimal('amount', 10, 2);
            $table->enum('method', ['CASH', 'GCASH', 'QRPH', 'BANK', 'WAIVED']);
            $table->string('reference')->nullable();
            $table->text('qrph_raw')->nullable();
            $table->timestamp('paid_at')->nullable();
            $table->unsignedBigInteger('verified_by')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('barangay_request_payments');
        Schema::dropIfExists('barangay_document_requests');
        Schema::dropIfExists('barangay_request_types');
    }
}
