<?php

namespace Tests\Feature;

use App\Http\Controllers\WithdrawController;
use App\Models\User;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;

class WithdrawVerifiedMemberRestrictionTest extends TestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        $this->createTables();
    }

    public function test_unverified_user_sees_withdraw_block_message_in_form_page(): void
    {
        $user = User::factory()->create([
            'is_active' => 0,
            'balance' => 250,
        ]);

        $this->actingAs($user);
        $response = app(WithdrawController::class)->create();
        $data = $response->getData();

        $this->assertSame('user.withdraw.form', $response->getName());
        $this->assertFalse((bool) $data['canWithdraw']);
        $this->assertSame(
            'আনভেরিফাইড মেম্বার উইথড্র করতে পারবে না। আগে একাউন্ট ভেরিফাই করুন।',
            $data['withdrawBlockMessage']
        );
    }

    public function test_unverified_user_cannot_submit_withdraw_request(): void
    {
        $user = User::factory()->create([
            'is_active' => 0,
            'balance' => 250,
        ]);

        $response = $this
            ->actingAs($user)
            ->from(route('withdraw.form'))
            ->post(route('withdraw.submit'), [
                'method' => 1,
                'type' => 1,
                'number' => '01712345678',
                'amount' => 100,
                'password' => 'password',
            ]);

        $response->assertRedirect(route('withdraw.form'));
        $response->assertSessionHas('error', 'আনভেরিফাইড মেম্বার উইথড্র করতে পারবে না। আগে একাউন্ট ভেরিফাই করুন।');
        $this->assertSame(0, \App\Models\Withdraw::query()->count());
    }

    public function test_withdraw_held_user_sees_withdraw_hold_message_in_form_page(): void
    {
        $user = User::factory()->create([
            'is_active' => 1,
            'withdraw_hold' => 1,
            'withdraw_hold_reason' => 'KYC pending review',
            'balance' => 250,
        ]);

        $this->actingAs($user);
        $response = app(WithdrawController::class)->create();
        $data = $response->getData();

        $this->assertSame('user.withdraw.form', $response->getName());
        $this->assertFalse((bool) $data['canWithdraw']);
        $this->assertSame(
            'আপনার উইথড্র সাময়িকভাবে হোল্ড করা হয়েছে। কারণ: KYC pending review',
            $data['withdrawBlockMessage']
        );
    }

    public function test_withdraw_held_user_cannot_submit_withdraw_request(): void
    {
        $user = User::factory()->create([
            'is_active' => 1,
            'withdraw_hold' => 1,
            'withdraw_hold_reason' => 'KYC pending review',
            'balance' => 250,
        ]);

        $response = $this
            ->actingAs($user)
            ->from(route('withdraw.form'))
            ->post(route('withdraw.submit'), [
                'method' => 1,
                'type' => 1,
                'number' => '01712345678',
                'amount' => 100,
                'password' => 'password',
            ]);

        $response->assertRedirect(route('withdraw.form'));
        $response->assertSessionHas('error', 'আপনার উইথড্র সাময়িকভাবে হোল্ড করা হয়েছে। কারণ: KYC pending review');
        $this->assertSame(0, \App\Models\Withdraw::query()->count());
    }

    private function createTables(): void
    {
        Schema::dropIfExists('withdraws');
        Schema::dropIfExists('withdraw_types');
        Schema::dropIfExists('withdraw_methods');
        Schema::dropIfExists('settings');
        Schema::dropIfExists('users');

        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->string('mobile')->nullable();
            $table->string('referred_by')->nullable();
            $table->string('referral_code')->nullable();
            $table->decimal('balance', 12, 2)->default(0);
            $table->decimal('voucher_balance', 12, 2)->default(0);
            $table->boolean('is_active')->default(false);
            $table->boolean('is_banned')->default(false);
            $table->boolean('withdraw_hold')->default(false);
            $table->text('withdraw_hold_reason')->nullable();
            $table->timestamp('activated_at')->nullable();
            $table->timestamps();
        });

        Schema::create('settings', function (Blueprint $table) {
            $table->id();
            $table->string('key')->unique();
            $table->text('value')->nullable();
            $table->timestamps();
        });

        Schema::create('withdraw_methods', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });

        Schema::create('withdraw_types', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });

        Schema::create('withdraws', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable();
            $table->foreignId('method_id')->nullable();
            $table->foreignId('type_id')->nullable();
            $table->string('number')->nullable();
            $table->decimal('amount', 12, 2)->default(0);
            $table->decimal('charge', 12, 2)->default(0);
            $table->decimal('total', 12, 2)->default(0);
            $table->string('transaction_id')->nullable();
            $table->string('status')->default('pending');
            $table->timestamps();
        });
    }
}
