<?php

namespace Tests\Feature;

use App\Models\User;
use App\Models\VendorProfile;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class VendorPaymentSettingsTest extends TestCase
{
    use RefreshDatabase;

    protected User $vendor;

    protected function setUp(): void
    {
        parent::setUp();
        File::put(storage_path('installed'), '{"installed": true}');
        Storage::fake('public');

        $this->vendor = User::create([
            'name' => 'Rajesh Mehta',
            'email' => 'rajesh@visionoptics.com',
            'phone' => '9876543210',
            'role' => 'vendor',
            'password' => bcrypt('Vendor@123'),
            'status' => true,
        ]);

        VendorProfile::create([
            'user_id' => $this->vendor->id,
            'vendor_name' => 'Vision Optics Ahmedabad',
            'city' => 'Ahmedabad',
            'is_active' => true,
        ]);
    }

    public function test_vendor_can_view_payment_settings_page(): void
    {
        $response = $this->actingAs($this->vendor)->get('/vendor/profile');
        $response->assertStatus(200);
        $response->assertSee('Store UPI ID / VPA');
        $response->assertSee('Bank Account Details');
    }

    public function test_vendor_can_save_upi_qr_and_bank_details(): void
    {
        $file = UploadedFile::fake()->create('my_store_upi_qr.png', 100, 'image/png');

        $response = $this->actingAs($this->vendor)->post('/vendor/profile', [
            'vendor_name' => 'Vision Optics Pro',
            'name' => 'Rajesh Mehta',
            'phone' => '9876543210',
            'city' => 'Ahmedabad',
            'whatsapp_number' => '+919876543210',
            'upi_id' => 'visionoptics@upi',
            'bank_name' => 'HDFC Bank',
            'account_number' => '50100987654321',
            'ifsc_code' => 'HDFC0001234',
            'account_holder_name' => 'Vision Optics Pro',
            'payment_instructions' => 'Scan QR to pay and WhatsApp screenshot for delivery.',
            'upi_qr_code' => $file,
        ]);

        $response->assertRedirect();
        $this->assertDatabaseHas('vendor_profiles', [
            'user_id' => $this->vendor->id,
            'vendor_name' => 'Vision Optics Pro',
            'upi_id' => 'visionoptics@upi',
            'bank_name' => 'HDFC Bank',
            'account_number' => '50100987654321',
            'ifsc_code' => 'HDFC0001234',
        ]);
    }

    public function test_vendor_can_update_payment_details_via_api(): void
    {
        $token = $this->vendor->createToken('test-token')->plainTextToken;

        $response = $this->withHeader('Authorization', 'Bearer ' . $token)
            ->postJson('/api/v1/vendor/profile', [
                'vendor_name' => 'Vision Optics Mobile',
                'upi_id' => 'rajesh@okaxis',
                'bank_name' => 'SBI Bank',
                'account_number' => '1234567890',
                'ifsc_code' => 'SBIN0001111',
            ]);

        $response->assertStatus(200);
        $response->assertJson(['success' => true]);
        $this->assertDatabaseHas('vendor_profiles', [
            'user_id' => $this->vendor->id,
            'upi_id' => 'rajesh@okaxis',
            'bank_name' => 'SBI Bank',
        ]);
    }

    public function test_customer_can_view_vendor_payment_details_via_api(): void
    {
        $this->vendor->vendorProfile()->update([
            'upi_id' => 'visionoptics@upi',
            'bank_name' => 'HDFC Bank',
            'payment_instructions' => 'Pay via UPI and WhatsApp receipt.',
        ]);

        $response = $this->getJson('/api/v1/customer/vendors/' . $this->vendor->id);
        $response->assertStatus(200);
        $response->assertJsonPath('data.vendor.vendor_profile.upi_id', 'visionoptics@upi');
        $response->assertJsonPath('data.vendor.vendor_profile.bank_name', 'HDFC Bank');
    }
}
