<?php

use App\Database\Seeds\DatabaseSeeder;
use App\Models\AcademyGroupModel;
use App\Models\AcademyLevelModel;
use App\Models\ChildModel;
use App\Models\CoachModel;
use CodeIgniter\Shield\Models\UserModel;
use CodeIgniter\Shield\Test\AuthenticationTesting;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use CodeIgniter\Test\FeatureTestTrait;

/**
 * @internal
 */
final class ChildrenFeatureTest extends CIUnitTestCase
{
    use AuthenticationTesting;
    use DatabaseTestTrait;
    use FeatureTestTrait;

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

    public function testCoachIndexIsScopedAndForeignProfileIsForbidden(): void
    {
        $coach   = $this->findUserByEmail('andrei@academia.local');
        $mariaId = (int) (model(ChildModel::class)->where('full_name', 'Maria Enache')->first()['id'] ?? 0);

        $this->actingAs($coach);

        $index = $this->withSession($_SESSION)->get('/children');

        $index->assertOK();
        $index->assertSee('Luca Marin');
        $index->assertDontSee('Maria Enache');

        $profile = $this->withSession($_SESSION)->get('/children/' . $mariaId);

        $profile->assertStatus(403);
    }

    public function testAdminCanCreateChildWithGuardianAndAssignments(): void
    {
        $admin = $this->findUserByEmail('admin@academia.local');
        $level = model(AcademyLevelModel::class)->orderBy('sort_order', 'ASC')->first();
        $group = model(AcademyGroupModel::class)->orderBy('id', 'ASC')->first();
        $coach = model(CoachModel::class)->orderBy('id', 'ASC')->first();

        $this->actingAs($admin);

        $response = $this->withSession($_SESSION)->post('/children', [
            'first_name' => 'Mina',
            'last_name' => 'Georgescu',
            'birth_date' => '2016-02-14',
            'gender' => 'female',
            'academy_level_id' => (string) $level['id'],
            'academy_group_id' => (string) $group['id'],
            'primary_coach_id' => (string) $coach['id'],
            'first_class_at' => '2026-04-10',
            'status' => 'active',
            'has_siblings' => '1',
            'notes' => 'Copil nou introdus prin feature test.',
            'guardian_name' => 'Dana Georgescu',
            'guardian_phone' => '0740 555 777',
            'guardian_email' => 'dana.georgescu@example.test',
        ]);

        $response->assertRedirect();
        $this->seeInDatabase('children', ['full_name' => 'Mina Georgescu', 'status' => 'active']);

        $child = model(ChildModel::class)->where('full_name', 'Mina Georgescu')->first();

        $this->assertNotNull($child);
        $this->seeInDatabase('parents', [
            'id' => $child['primary_guardian_id'],
            'full_name' => 'Dana Georgescu',
        ]);
        $this->seeInDatabase('child_coaches', [
            'child_id' => $child['id'],
            'coach_id' => $coach['id'],
            'is_primary' => 1,
        ]);

        $profile = $this->withSession($_SESSION)->get('/children/' . $child['id']);

        $profile->assertOK();
        $profile->assertSee('Mina Georgescu');
        $profile->assertSee('Dana Georgescu');
    }

    private function findUserByEmail(string $email)
    {
        return model(UserModel::class)->findByCredentials(['email' => $email]);
    }
}
