<?php

namespace Modules\School\Services;

use Illuminate\Http\Request;
use Modules\School\Interfaces\SchoolRepositoryInterface;
use Modules\School\Models\School;

class SchoolService
{
    private SchoolRepositoryInterface $schoolRepository;

    public function __construct(SchoolRepositoryInterface $schoolRepository)
    {
        $this->schoolRepository = $schoolRepository;
    }

    public function get(array $request = [])
    {
        $builder = $this->schoolRepository->get($request);

        if (@$request['title']) {
            $builder->where('sd.title', 'LIKE', '%' . $request['title'] . '%');
        }

        if (@$request['code']) {
            $builder->where('schools.code', $request['code']);
        }

        if (@$request['city_id']) {
            $builder->where('schools.city_id', $request['city_id']);
        }

        if (@$request['date_from']) {
            $builder->whereDate('schools.created_at', '>=', $request['date_from']);
        }

        if (@$request['date_to']) {
            $builder->whereDate('schools.created_at', '<=', $request['date_to']);
        }

        if (isset($request['status'])) {
            if (@$request['status'] == 0) {
                $builder->onlyTrashed();
            }
        }

        if (@$request['paginate']) {
            $schools = $builder->paginate(50);
        } else if (@$request['api']) {
            $schools = $builder;
        } else {
            $schools = $builder->cursor();
        }
        return $schools;
    }
    public function checkItem($id)
    {
        $school = $this->schoolRepository->checkItem($id);
        return $school;
    }

    public function getItemByCode($code)
    {
        return $this->schoolRepository->getItemByCode($code);
    }

    public function getItem($id)
    {
        $school = $this->checkItem($id);
        $schoolDescription = $this->schoolRepository->getItem($id);
        foreach ($schoolDescription as $row) {
            $school[$row->local] = $row;
        }
        return $school;
    }

    public function getItemTrashed($id)
    {
        $school = $this->schoolRepository->getItemTrashed($id);
        return $school;
    }

    public function generateCode()
    {
        do {
            $code = rand(100, 99999999999);
        } while (!is_null($this->getItemByCode($code)));
        return $code;
    }

    public function create(array $request): School
    {
        $request['code'] = $this->generateCode();
        $school = $this->schoolRepository->create($request);
        $this->schoolRepository->saveData($request, $school->id);
        return $school;
    }
    public function update(array $request, int $id): School
    {
        $school = $this->checkItem($id);


        $school = $this->schoolRepository->update($request, $school);
        $this->schoolRepository->saveData($request, $school->id);
        return $school;
    }
    public function destroy(int $id): void
    {
        $school = $this->checkItem($id);
        if ($school) {
            $this->schoolRepository->destroy($school);
        } else {
            $school = $this->getItemTrashed($id);
            $this->schoolRepository->restore($school);
        }
    }
    public function destroyAll(array $request): void
    {
        if ($request['status'] == 1) {
            $this->schoolRepository->destroyAll($request['ids']);
        } else {
            $this->schoolRepository->restoreAll($request['ids']);
        }
    }
}
