<?php

namespace Tests\Feature\Api;

use App\Models\User;
use App\Modules\Tenant\Models\Company;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class CompanySettingsApiTest extends TestCase
{
    use RefreshDatabase;

    public function test_owner_can_update_company_settings(): void
    {
        [$user, $company] = $this->createCompanyMember();

        Sanctum::actingAs($user);

        $this->withHeader('X-Company-ID', (string) $company->id)
            ->patchJson('/api/v1/tenant/settings', [
                'name' => 'ABC Super Shop',
                'email' => 'owner@example.com',
                'phone' => '01700000000',
                'address' => 'Dhaka',
                'currency' => 'bdt',
                'timezone' => 'Asia/Dhaka',
                'settings' => [
                    'default_tax_rate' => 7.5,
                    'invoice_prefix' => 'bill',
                    'pos_prefix' => 'sale',
                    'purchase_prefix' => 'buy',
                    'expense_prefix' => 'cost',
                    'payment_terms_days' => 15,
                    'low_stock_alerts' => true,
                    'invoice_due_alerts' => true,
                    'crm_follow_up_alerts' => false,
                ],
            ])
            ->assertOk()
            ->assertJsonPath('data.name', 'ABC Super Shop')
            ->assertJsonPath('data.currency', 'BDT')
            ->assertJsonPath('data.settings.default_tax_rate', 7.5)
            ->assertJsonPath('data.settings.invoice_prefix', 'BILL')
            ->assertJsonPath('data.settings.expense_prefix', 'COST')
            ->assertJsonPath('data.settings.crm_follow_up_alerts', false);

        $this->assertDatabaseHas('companies', [
            'id' => $company->id,
            'name' => 'ABC Super Shop',
            'currency' => 'BDT',
            'timezone' => 'Asia/Dhaka',
        ]);
    }

    public function test_member_cannot_update_company_settings(): void
    {
        [$user, $company] = $this->createCompanyMember(role: 'member');

        Sanctum::actingAs($user);

        $this->withHeader('X-Company-ID', (string) $company->id)
            ->patchJson('/api/v1/tenant/settings', [
                'name' => 'Blocked Update',
                'currency' => 'BDT',
                'timezone' => 'Asia/Dhaka',
            ])
            ->assertForbidden();
    }

    public function test_current_company_response_includes_settings(): void
    {
        [$user, $company] = $this->createCompanyMember();
        $company->update([
            'settings' => [
                'default_tax_rate' => 5,
                'invoice_prefix' => 'INV',
            ],
        ]);

        Sanctum::actingAs($user);

        $this->withHeader('X-Company-ID', (string) $company->id)
            ->getJson('/api/v1/tenant/current')
            ->assertOk()
            ->assertJsonPath('data.company.settings.default_tax_rate', 5)
            ->assertJsonPath('data.company.settings.invoice_prefix', 'INV');
    }

    /**
     * @return array{0: User, 1: Company}
     */
    private function createCompanyMember(string $companyName = 'ABC Grocery', string $slug = 'abc-grocery', string $role = 'owner'): array
    {
        $user = User::factory()->create();
        $company = Company::query()->create([
            'name' => $companyName,
            'slug' => $slug,
            'status' => 'active',
        ]);

        $company->users()->attach($user->id, [
            'role' => $role,
            'joined_at' => now(),
        ]);

        return [$user, $company];
    }
}
