<?php

namespace App\Services;

use App\Models\BoardModel;
use App\Helpers\BoardHelper;
use App\Forms\BoardForm;
use App\Entities\BoardEntity;

class BoardService extends CommonService
{
    protected string $formClass = BoardForm::class;
    protected string $helperClass = BoardHelper::class;

    public function __construct(BoardModel $model)
    {
        parent::__construct($model);
        $this->addClassPaths('Board');
    }
    public function getEntityClass(): string
    {
        return BoardEntity::class;
    }
    //기본 기능부분
    protected function getEntity_process(mixed $entity): BoardEntity
    {
        return $entity;
    }
    //List 검색용
    //FormFilter 조건절 처리
    //검색어조건절처리

    //추가기능부분
    //Category별 최근 $limit갯수만큼 게시물
    public function getLatest(string $category): array
    {
        //관리자정보
        $userEntities = service('userservice')->getEntities();
        $datas = [];
        foreach ($this->getEntities(['category' => $category, 'status' => STATUS['AVAILABLE']]) as $entity) {
            $datas[] = [
                'uid' => $entity->getPK(),
                'title' => $entity->getTitle(),
                'url' => "/admin/board/view/{$entity->getPK()}",
                'created_at' => date('Y-m-d H:i', strtotime($entity->getCreatedAT())),
                'user' => $userEntities[$entity->getUserUid()]->getTitle(),
            ];
        }
        return $datas;
    }
    //요청업무 갯수(Home에서 사용)
    public function getRequestTaskCount(int $worker_uid): int
    {
        return count($this->getEntities([
            'category' => BOARD['CATEGORY']['REQUESTTASK'],
            'worker_uid' => $worker_uid,
            'status' => STATUS['AVAILABLE']
        ]));
    }
}
