<?php

namespace Modules\Target\Repositories;

use Modules\Target\Interfaces\TargetRepositoryInterface;
use Modules\Target\Models\Target;
use Modules\Target\Models\TargetTime;

class TargetRepository implements TargetRepositoryInterface
{
    public function get(array $request)
    {
        return Target::query()->select();
    }

    public function getItem(int $id)
    {
        return Target::find($id);
    }

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

    public function getItemTrashed(int $id)
    {
        $target = Target::onlyTrashed()->find($id);
        return $target ;
    }

    public function create(array $request): Target
    {
        return Target::create($request);
    }

    public function update(array $request, ?Target $target): Target
    {
        $target->update($request);

        return $target;
    }

    public function getMonths(array $request)
    {
        return Target::where('product_id',$request['product_id'])
            ->where('employee_id',$request['employee_id'])
            ->where('year',$request['year'])
            ->pluck('month')->toArray();
    }


    public function getTarget(array $request )
    {
        return Target::query()
                ->where('employee_id',$request['employee_id'])
                ->where('year',$request['year'])
                ->where('month',$request['month'])
                ->where('product_id',$request['product_id'])
                ->first();
    }
    public function deleteYearMonth(array $request )
    {
        Target::query()
        ->where('employee_id',$request['employee_id'])
        ->where('year',$request['year'])
        ->whereIn('month',$request['month'])
        ->whereIn('product_id',$request['product_id'])
        ->delete();
    }

    /**
     * Remove the specified tag from storage.
     * @param Target $tag
     * @return void
     */
    public function destroy(Target $target) :void
    {
        $target->delete();
    }

    /**
     * Remove several tags by IDs.
     *
     * @param array $request
     * @return void
     */
    public function destroyAll(array $ids) :void
    {
         Target::whereIn('id', $ids)->delete();
    }

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

    /**
     * Restore several tags by IDs.
     *
     * @param array $request
     * @return void
     */
    public function restoreAll(array $ids) :void
    {
        Target::whereIn('id', $ids)->onlyTrashed()->restore();
    }
}
