<?php

namespace Tests\Feature;

use App\Models\Branch;
use App\Models\Business;
use App\Models\Customer;
use App\Models\Guarantor;
use App\Models\NextOfKin;
use App\Models\User;
use Database\Seeders\DatabaseSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class PhoneNumberWorkflowTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed(DatabaseSeeder::class);
    }

    public function test_changing_business_phone_country_code_rebases_existing_canonical_numbers(): void
    {
        $business = Business::firstOrFail();
        $business->update(['phone' => '0712345678', 'phone_alt' => '0612345678']);

        $branch = Branch::create([
            'business_id' => $business->id,
            'name' => 'Phone Prefix Branch',
            'phone' => '0755000000',
            'status' => 'active',
        ]);

        $staff = User::factory()->create([
            'username' => 'phone_prefix_staff',
            'phone' => '0622000000',
            'status' => 'active',
            'primary_branch_id' => $branch->id,
        ]);

        $customer = Customer::factory()->create([
            'branch_id' => $branch->id,
            'phone' => '0711000000',
            'alternative_phone' => '0611000000',
        ]);

        $kin = NextOfKin::create([
            'customer_id' => $customer->id,
            'full_name' => 'Primary Kin',
            'relationship' => 'Sibling',
            'phone' => '0744000000',
            'is_primary' => true,
        ]);

        $guarantor = Guarantor::create([
            'customer_id' => $customer->id,
            'full_name' => 'Primary Guarantor',
            'phone' => '0633000000',
            'status' => 'active',
        ]);

        $admin = User::where('username', 'admin')->firstOrFail();
        $this->actingAs($admin)->put(route('admin.business-settings.update'), [
            'name' => $business->name,
            'country' => $business->country,
            'phone_country_code' => '254',
            'currency' => $business->currency,
            'currency_symbol' => $business->currency_symbol,
            'number_format' => $business->number_format,
            'financial_year_start_month' => $business->financial_year_start_month,
            'accounting_period' => $business->accounting_period,
            'default_interest_method' => $business->default_interest_method,
            'default_penalty_settings' => $business->default_penalty_settings,
        ])->assertRedirect()->assertSessionHasNoErrors();

        $this->assertSame('254', $business->fresh()->phone_country_code);
        $this->assertSame('254712345678', $business->fresh()->phone);
        $this->assertSame('254612345678', $business->fresh()->phone_alt);
        $this->assertSame('254755000000', $branch->fresh()->phone);
        $this->assertSame('254622000000', $staff->fresh()->phone);
        $this->assertSame('254711000000', $customer->fresh()->phone);
        $this->assertSame('254611000000', $customer->fresh()->alternative_phone);
        $this->assertSame('254744000000', $kin->fresh()->phone);
        $this->assertSame('254633000000', $guarantor->fresh()->phone);
    }
}
