<?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()
    {
        $branchId = $this->getActiveBranchId();
        $query = Evaluation::query()
            ->when($branchId, fn ($q) => $q->whereHas('group', fn ($g) => $g->where('branch_id', $branchId)))
            ->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(),
        ]);
    }
}
