<?php

namespace App\Livewire\Evaluations;

use App\Domain\Shared\Traits\UsesBranchScope;
use App\Domain\Training\Enums\EvaluationStatus;
use App\Domain\Training\Models\Evaluation;
use App\Domain\Training\Models\TrainingGroup;
use App\Domain\Training\Models\TrainingProgram;
use App\Livewire\Concerns\AppliesRoleScope;
use App\Livewire\Concerns\WithSorting;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Attributes\Url;
use Livewire\Component;
use Livewire\WithPagination;

#[Layout('layouts.app')]
#[Title('التقييمات')]
class EvaluationList extends Component
{
    use WithPagination, AppliesRoleScope, WithSorting, UsesBranchScope;

    protected string $scopePermission = 'evaluations.list';

    #[Url]
    public string $search = '';

    #[Url]
    public string $status = '';

    #[Url]
    public string $programFilter = '';

    #[Url]
    public string $groupId = '';

    public function mount(): void
    {
        $this->authorize('evaluations.list');
        if (!request()->has('sortBy')) {
            $this->sortBy = 'evaluation_date';
        }
        if (!request()->has('sortDir')) {
            $this->sortDir = 'desc';
        }
    }

    public function updatedSearch(): void
    {
        $this->resetPage();
    }

    public function updatedStatus(): void
    {
        $this->resetPage();
    }

    public function updatedProgramFilter(): void
    {
        $this->groupId = '';
        $this->resetPage();
    }

    public function updatedGroupId(): void
    {
        $this->resetPage();
    }

    public function render()
    {
        // Evaluation carries its own branch_id and the BranchScope, so the
        // list narrows itself. The old whereHas('group', branch_id) attributed
        // an evaluation through its group — a wrong reading twice over: the
        // group is nullable, so a group-less evaluation vanished, and a group
        // moved to another branch took its evaluations' history with it.
        $query = Evaluation::query()
            ->with(['participant.person', 'group.program', 'evaluator'])
            ->when($this->search, fn ($q) => $q->whereHas('participant.person', fn ($pq) => $pq->where('name_ar', 'ilike', "%{$this->search}%")))
            ->when($this->status, fn ($q) => $q->where('status', $this->status))
            ->when($this->programFilter, fn ($q) => $q->whereHas('group', fn ($gq) => $gq->where('training_program_id', $this->programFilter)))
            ->when($this->groupId, fn ($q) => $q->where('training_group_id', $this->groupId))
            ->orderBy($this->sortBy, $this->sortDir);

        $this->applyRoleScope($query);

        $groups = TrainingGroup::query()
            ->when($this->programFilter, fn ($q) => $q->where('training_program_id', $this->programFilter))
            ->orderBy('name_ar')
            ->get(['id', 'name_ar']);

        $programs = TrainingProgram::where('status', 'active')
            ->orderBy('name_ar')
            ->get(['id', 'name_ar']);

        return view('livewire.evaluations.evaluation-list', [
            'evaluations' => $query->paginate(20),
            'programs' => $programs,
            'groups' => $groups,
            'statuses' => EvaluationStatus::cases(),
        ]);
    }
}
