<?php

namespace Tests\Feature;

use App\Models\Customer;
use App\Models\Sale;
use App\Models\User;
use Database\Seeders\DatabaseSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CustomerDetailTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed(DatabaseSeeder::class);
        $this->actingAs(User::where('username', 'admin')->firstOrFail());
    }

    public function test_customer_detail_shows_invoice_totals_pending_amount_and_invoice_history(): void
    {
        $customer = Customer::where('customer_code', '403')->firstOrFail();
        $sale = Sale::where('customer_id', $customer->id)->where('status', 'finalized')->firstOrFail();

        $this->get(route('customers.show', $customer))
            ->assertOk()
            ->assertSee($customer->name_en)
            ->assertSee('Total sales')
            ->assertSee('Total paid')
            ->assertSee('Pending invoices')
            ->assertSee(number_format((float) $sale->grand_total, 2))
            ->assertSee(number_format((float) $sale->balance_total, 2))
            ->assertSee($sale->invoice_no)
            ->assertSee(route('sales.show', $sale), false);

        $this->get(route('customers.index'))
            ->assertOk()
            ->assertSee(route('customers.show', $customer), false);
    }
}
