<?php

declare(strict_types=1);

namespace Seka\Vzdelavanie\Http\Controllers\Api\Kniznica;

use Exception;
use Illuminate\Support\Facades\Auth;
use Seka\Vzdelavanie\Models\Kniznica\Sekcia;
use Seka\Vzdelavanie\Models\Kniznica\Ucebnica;
use Illuminate\Validation\UnauthorizedException;
use Seka\Vzdelavanie\Http\Controllers\Api\Controller;
use Seka\Vzdelavanie\Http\Resources\OblubeneResource;
use Seka\Vzdelavanie\Models\Pouzivatel\Kniznica\Oblubene;
use Seka\Vzdelavanie\Http\Requests\Api\OblubeneStoreRequest;
use Seka\Vzdelavanie\Models\Pouzivatel\Kniznica\OblubeneModelTypeEnum;

/**
 * Class OblubeneController
 *
 * @package Seka\Vzdelavanie\Http\Controllers\Api\Kniznica
 */
final class OblubeneController extends Controller
{
    /**
     * @param \Seka\Vzdelavanie\Http\Requests\Api\OblubeneStoreRequest $request
     *
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Builder[][]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Collection[]|\Illuminate\Database\Eloquent\Model[]|\Illuminate\Support\Collection|mixed|null[]|\Seka\Vzdelavanie\Models\Kniznica\Ucebnica|null
     * @throws \Exception
     */
    public function store(OblubeneStoreRequest $request)
    {
        try {
            $oblubene = new Oblubene();
            $oblubene->pouzivatel()->associate(Auth::user());
            $modelTypeEnum = new OblubeneModelTypeEnum($request->json('typ'));

            if ($modelTypeEnum->equals(OblubeneModelTypeEnum::MODEL_TYPE_UCEBNICA())) {
                /** @var Ucebnica $ucebnica */
                $ucebnica = Ucebnica::query()->where('id', $request->json('id'))->firstOrFail();
                $ucebnica->oblubene()->save($oblubene);
            } elseif ($modelTypeEnum->equals(OblubeneModelTypeEnum::MODEL_TYPE_SEKCIA())) {
                /** @var Ucebnica $ucebnica */
                $sekcia = Sekcia::query()->where('id', $request->json('id'))->firstOrFail();
                $sekcia->oblubene()->save($oblubene);
            } else {
                throw new Exception('Neznámi druh modelu');
            }
        } catch (Exception $exc) {
            return response()->json([
                'error' => $exc->getMessage() ?: class_basename($exc),
            ], 500);
        }

        return OblubeneResource::make($oblubene->fresh());
    }

    /**
     * @param int $id
     *
     * @return bool|mixed|null
     * @throws \Exception
     */
    public function destroy(int $id)
    {
        try {
            /** @var Oblubene $oblubene */
            $oblubene = Oblubene::query()->where('id', $id)->firstOrFail();

            if ($oblubene->pouzivatel->getKey() !== Auth::id()) {
                throw new UnauthorizedException('Nemáte oprávnenie na odstránenie obľúbenej položky iného používateľa.');
            }

            $oblubene->delete();
        } catch (Exception $exc) {
            return response()->json([
                'error' => $exc->getMessage() ?: class_basename($exc),
            ], 500);
        }

        return response(null);
    }
}
