<?php

namespace Modules\Activity\Http\Controllers;

use App\Http\Controllers\BaseCrudController;
use Illuminate\Http\JsonResponse;

class ActivityTypeController extends BaseCrudController
{
    protected $modelClass = \Modules\Activity\Entities\ActivityType::class;

    protected $resourceClass = \Modules\Activity\Transformers\ActivityTypeResource::class;

    protected $requestClass = \Modules\Activity\Http\Requests\ActivityTypeRequest::class;

    protected $resourceName = 'activity type';

    protected $resourceNamePlural = 'activity types';

    public function __construct(
        \Modules\Activity\Repositories\Contracts\ActivityTypeRepositoryInterface $repository,
    ) {
        parent::__construct($repository);
    }

    protected function getItemName($item): string
    {
        return $item->name ?? $item->id;
    }

    protected function checkDependencies($activityType): ?JsonResponse
    {
        if ($activityType->activities()->count() > 0) {
            return response()->json([
                'success' => false,
                'message' => 'Jenis aktiviti tidak boleh dipadam kerana masih digunakan oleh aktiviti.',
            ], 422);
        }

        return null;
    }
}
