<?php

namespace App\Http\Controllers;

use Illuminate\Support\Arr;
use App\Services\VersionService;
use App\Http\Requests\Learning\VersionStore;
use App\Repositories\FilterItemsAssociation;

class VersionController extends Controller
{

    private $versionService;

    public function __construct(VersionService $versionService)
    {
        $this->versionService = $versionService;
        $this->middleware('auth:sanctum');
    }

    public function index()
    {
        $versions = $this->versionService->getAll();

        return response()->json($versions, 201);
    }

    public function store(VersionStore $request)
    {
        $isSuperAdmin = auth()->user()->hasRole('super_admin');
        $isAdmin = auth()->user()->hasRole('admin');
        $isOperator = auth()->user()->hasRole('operator');

        if (!$isSuperAdmin && !$isAdmin && !$isOperator) {
            return response()->json(['message' => 'You have no rights to do this'], 401);
        }

        if ($request['filter_items']) {
            $filter_items = Arr::collapse($request['filter_items']);
            $filter_items = Arr::flatten($filter_items);
        }

        if ($request['checklists_selected']) {
            $checklists_selected = $request['checklists_selected'];
        }

        $data = Arr::except($request->validated(), ['filter_items', 'checklists_selected']);

        $version = $this->versionService->save($data);

        if (isset($filter_items) && $filter_items) {

            $version->filters()->delete();

            foreach ($filter_items as $filter_item_id) {
                $filter_association = new FilterItemsAssociation();
                $filter_association->filter_item_id = $filter_item_id;
                $version->filters()->save($filter_association);
            }
        }

        if (isset($checklists_selected) && $checklists_selected) {

            // Fetch all checklists ids already stored
            $checklists_ids_stored = $version->checklists()->pluck('checklist_id');

            // If an id from $checklists_ids_stored is not present in the uploaded checklists_selected, delete it
            foreach ($checklists_ids_stored as $checklist_id_stored) {

                if (!in_array($checklist_id_stored, $checklists_selected)) {
                    $version->checklists()->where('checklist_id', $checklist_id_stored)->delete();
                }
            }

            // If a checklist from the uploaded is not present in $version->checklists(), create 
            foreach ($checklists_selected as $checklist_selected) {

                if (!in_array($checklist_selected, $checklists_ids_stored->toArray())) {

                    $version->checklists()->create([
                        'user_id' => auth()->user()->id,
                        'version_id' => $version->id,
                        'checklist_id' => $checklist_selected,
                    ]);
                }
            }
        }

        return response()->json($version->load(['filters', 'checklists']), 201);
    }

    public function show($id)
    {
        $version = $this->versionService->get($id);

        return response()->json($version);
    }

    public function destroy($id)
    {
        $isSuperAdmin = auth()->user()->hasRole('super_admin');
        $isAdmin = auth()->user()->hasRole('admin');
        $isOperator = auth()->user()->hasRole('operator');

        if (!$isSuperAdmin && !$isAdmin && !$isOperator) {
            return response()->json(['message' => 'You have no rights to do this'], 401);
        }

        $this->versionService->delete($id);
        return response()->json(null, 204);
    }
}
