<?php

use App\Database\Seeds\DatabaseSeeder;
use CodeIgniter\Shield\Models\UserModel;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;

/**
 * @internal
 */
final class DashboardServiceTest extends CIUnitTestCase
{
    use DatabaseTestTrait;

    protected $migrate = true;
    protected $refresh = true;
    protected $seed = DatabaseSeeder::class;
    protected $DBGroup = 'tests';
    protected $namespace = ['CodeIgniter\\Shield', 'CodeIgniter\\Settings', 'App'];

    public function testDashboardMetricsUseSeededData(): void
    {
        $admin = model(UserModel::class)->findByCredentials(['email' => 'admin@academia.local']);
        $data  = service('dashboard')->getDashboardData([], $admin);

        $this->assertSame(13, $data['kpis'][0]['value']);
        $this->assertSame(4, $data['kpis'][2]['value']);
        $this->assertSame(4, $data['kpis'][3]['value']);
        $this->assertNotEmpty($data['recentEvaluations']);
        $this->assertContains('ready', array_column($data['alerts'], 'type'));
        $this->assertContains('attendance', array_column($data['alerts'], 'type'));
        $this->assertContains('overdue', array_column($data['alerts'], 'type'));
    }

    public function testCoachDashboardLocksCoachFilter(): void
    {
        $coach = model(UserModel::class)->findByCredentials(['email' => 'andrei@academia.local']);
        $data  = service('dashboard')->getDashboardData([], $coach);

        $this->assertTrue($data['filters']['coach_locked']);
        $this->assertSame(['Andrei Popescu'], $data['childrenPerCoach']['categories']);
        $this->assertSame([4], $data['childrenPerCoach']['series']);
    }
}
