<?php

namespace App\Services;

use App\Models\AttendanceCommentTemplate;

class AttendanceCommentTemplateService
{
    public function list(bool $activeOnly = false): array
    {
        $query = AttendanceCommentTemplate::query();

        if ($activeOnly) {
            $query->where('is_active', 1);
        }

        return $query
            ->orderBy('min_score')
            ->get()
            ->toArray();
    }

    public function findOrFail(int $id): array
    {
        return AttendanceCommentTemplate::query()
            ->findOrFail($id)
            ->toArray();
    }

    public function create(array $data): array
    {
        return AttendanceCommentTemplate::query()
            ->create($data)
            ->toArray();
    }

    public function update(int $id, array $data): array
    {
        $template = AttendanceCommentTemplate::query()->findOrFail($id);
        $template->fill($data);
        $template->save();

        return $template->toArray();
    }

    public function delete(int $id): void
    {
        AttendanceCommentTemplate::query()->whereKey($id)->delete();
    }
}
