<?php

namespace App\Http\Controllers\Api\Companies;

use App\Http\Controllers\Controller;
use App\Models\Companies\Department;
use App\Transformers\Companies\DepartmentTransformer;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class DepartmentsController extends Controller {
    protected Department $model;

    public function __construct(Department $model) {
        $this->model = $model;
    }

    public function index(Request $request): JsonResponse {
        $query = $this->model->query();
        $paginator = $query->paginate(config('app.pagination_limit'));
        return fractal($paginator, new DepartmentTransformer())->respond();
    }

    public function show($id): JsonResponse {
        $model = $this->model->byUuid($id)->firstOrFail();
        return fractal($model, new DepartmentTransformer())->respond();
    }

    public function my(Request $request): JsonResponse {
        return fractal($request->user()->selectedMember->department, new DepartmentTransformer())->respond();
    }

    public function destroy(Request $request, $uuid): JsonResponse {
        $model = $this->model->byUuid($uuid)->firstOrFail();
        $model->delete();
        return response()->json(null, 204);
    }

}
