<?php

declare(strict_types=1);

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

use Exception;
use Illuminate\Support\Facades\Auth;
use Seka\Vzdelavanie\Enums\ModelsTypeEnum;
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;

/**
 * Class OblubeneController
 *
 * @package Seka\Vzdelavanie\Http\Controllers\Api\Frontend\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 ModelsTypeEnum($request->json('typ'));

            if ($modelTypeEnum->equals(ModelsTypeEnum::MODEL_TYPE_KNIZNICA_UCEBNY_MATERIAL())) {
                /** @var Ucebnica $ucebnica */
                $ucebnica = Ucebnica::query()->where('id', $request->json('id'))->firstOrFail();
                $ucebnica->oblubene()->save($oblubene);
            } elseif ($modelTypeEnum->equals(ModelsTypeEnum::MODEL_TYPE_KNIZNICA_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);
    }
}
