<?php

namespace Tests\Feature;

use App\Domain\Training\Models\TrainingGroup;
use App\Models\User;
use Tests\TestCase;

/**
 * The subscription column and the POS grid are Blade, so a wrong variable name
 * or a renamed method is a 500 at request time and nothing earlier catches it.
 *
 * Runs against a restored Postgres tenant; skips on the default SQLite suite,
 * where the schema does not exist:
 *
 *   DB_CONNECTION=pgsql DB_DATABASE=oc_sport_test ./vendor/bin/phpunit --filter GroupRosterAndPosRenderTest
 */
class GroupRosterAndPosRenderTest extends TestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        if (config('database.default') !== 'pgsql') {
            $this->markTestSkipped('Needs a restored Postgres tenant; see the class comment.');
        }
    }

    private function anAdmin(): User
    {
        $user = User::query()->whereHas('roles', fn ($q) => $q->where('name', 'admin'))->first()
            ?? User::query()->first();

        $this->assertNotNull($user, 'The restored tenant has no users to act as.');

        return $user;
    }

    public function test_the_group_roster_renders_and_names_the_subscription_column(): void
    {
        $group = TrainingGroup::withoutGlobalScopes()
            ->whereHas('enrollments', fn ($q) => $q->where('status', 'active'))
            ->first();

        $this->assertNotNull($group, 'No group with active enrollments in the restored tenant.');

        $response = $this->actingAs($this->anAdmin())->get(route('groups.show', $group));

        $response->assertOk();
        $response->assertSee('الاشتراك', escape: false);
        $response->assertDontSee('دليل ألوان الدفع', escape: false);
    }

    public function test_the_pos_terminal_renders_with_engine_prices(): void
    {
        $cashier = User::query()->get()->first(fn (User $u) => $u->can('pos.sell'));

        if (! $cashier) {
            $this->markTestSkipped('No user in the restored tenant may sell.');
        }

        $this->actingAs($cashier)->get(route('pos.terminal'))->assertOk();
    }
}
