<?php

namespace Modules\Unit\Repositories;

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Collection;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Unit\Interfaces\UnitRepositoryInterface;
use Modules\Unit\Models\Unit;
use Modules\Unit\Models\UnitDescription;
use Modules\Unit\Models\UnitPhoto;

class UnitRepository implements UnitRepositoryInterface
{
    public function get(array $request)
    {
        $builder = Unit::query()
            ->with(['type.currentDescription', 'user', 'area'=> function($query) {
                $query->with(['currentDescription', 'city.currentDescription']);
            }])
            ->join('unit_descriptions AS sd', 'units.id', 'sd.unit_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'units.*']);
        return $builder;
    }

    public function getItem(int $id)
    {
        return UnitDescription::where('unit_id', $id)
            ->join('languages', 'languages.id', 'unit_descriptions.language_id')
            ->join('units', 'units.id', 'unit_descriptions.unit_id')
            ->select(['unit_descriptions.*', 'languages.local','units.*'])->get();
    }

    public function mostActiveUserIdThisMonth()
    {
        return Unit::select('user_id')
        ->from('units')
        ->whereBetween('created_at', [
            now()->startOfMonth(),
            now()->endOfMonth(),
        ])
        ->groupBy('user_id')
        ->orderByRaw('COUNT(*) DESC')
        ->first()->user_id ?? null;
    }

    public function checkItem(int $id)
    {
        return Unit::find($id);
    }

    public function getItemByCode(string $code)
    {
        return Unit::whereCode($code)->first();
    }

    public function getItemTrashed(string $code)
    {
        $unit = Unit::onlyTrashed()->whereCode($code)->first();
        return $unit ;
    }

    public function create(array $request): Unit
    {
        $unit = Unit::create($request);
        return $unit;
    }

    public function update(array $request, ?Unit $unit): Unit
    {
        $unit->update($request);
        return $unit;
    }

    public function saveData(array $request, int $unit_id ) :void
    {
        UnitDescription::where('unit_id', $unit_id)->delete();
        foreach (languages() as $lang) {
            $data=[
                'unit_id'=> $unit_id,
                'language_id'=> $lang->id,
                'title'=> $request['title_'.$lang->local],
                'description'=> $request['description_'.$lang->local],
            ];
            UnitDescription::create($data);
        }
    }
    /**
     * Remove the specified unit from storage.
     * @param Unit $unit
     * @return void
     */
    public function destroy(Unit $unit) :void
    {
        $unit->delete();
    }

    /**
     * Remove several units by IDs.
     *
     * @param array $request
     * @return void
     */
    public function destroyAll(array $codes) :void
    {
         Unit::whereIn('code', $codes)->delete();
    }

    /**
     * Restore the specified unit from storage.
     *
     * @param Unit $unit
     * @return void
     */
    public function restore(Unit $unit) :void
    {
        $unit->restore();
    }

    /**
     * Restore several units by IDs.
     *
     * @param array $request
     * @return void
     */
    public function restoreAll(array $codes) :void
    {
        Unit::whereIn('code', $codes)->onlyTrashed()->restore();
    }

    public function saveImages(array $request, int $unit_id ) :void
    {
        UnitPhoto::where('unit_id', $unit_id)->delete();
        foreach ($request['images'] ?? [] as $image) {
            $data =
            [
                'unit_id'=> $unit_id,
                'image'=> $image,
            ];
            UnitPhoto::create($data);
        }
    }

    public function insertImages(array $data): bool
    {
        return UnitPhoto::insert($data);
    }

    public function getAllImages($unitId) : ?Collection
    {
        return UnitPhoto::where('unit_id', $unitId)->select('id', 'image')->get();
    }

    public function deleteAllImages($ids= []):void
    {
        UnitPhoto::whereIn('id', $ids)->delete();
    }
}
