<?php

namespace App\Repositories\Backend;

use App\Models\Quotation;
use Illuminate\Support\Facades\DB;
use App\Exceptions\GeneralException;
use App\Repositories\BaseRepository;
use App\Events\Backend\Quotation\QuotationUpdated;
use Illuminate\Pagination\LengthAwarePaginator;
use App\Events\Backend\Quotation\QuotationPermanentlyDeleted;
use App\Events\Backend\Quotation\QuotationRestored;


/**
 * Class UserRepository.
 */
class QuotationRepository extends BaseRepository
{
    /**
     * @return string
     */
    public function model()
    {
        return Quotation::class;
    }

    /**
     * @param array $where
     * @param date $date
     * @param int $paged
     *
     * @return mixed
     */
    public function getAllByCondition(array $where, $date=null, $paged=25)
    {
        if($date) {
            return $this->model
                ->where($where)
                ->whereDate('created_at', $date)
                ->paginate($paged);
        } else {
            return $this->model
                ->where($where)
                ->paginate($paged);
        }
    }

    /**
     *
     * @return int
     */
    public function getAllCount() : int
    {
        return $this->model
            ->count();
    }
    
    /**
     * @param int    $paged
     * @param string $orderBy
     * @param string $sort
     *
     * @return mixed
     */
    public function getStatusPaginated($paged, $orderBy, $sort, $status) : LengthAwarePaginator
    {
        return $this->model
            ->where('status',$status)
            ->orderBy($orderBy, $sort)
            ->paginate($paged);
    }

     /**
     * @param int    $paged
     * @param string $orderBy
     * @param string $sort
     *
     * @return LengthAwarePaginator
     */
    public function getActivePaginated($paged = 25, $orderBy = 'created_at', $sort = 'desc') : LengthAwarePaginator
    {
        return $this->model
            ->orderBy($orderBy, $sort)
            ->paginate($paged);
    }

     /**
     * @param int    $paged
     * @param string $orderBy
     * @param string $sort
     *
     * @return LengthAwarePaginator
     */
    public function getInactivePaginated($paged = 25, $orderBy = 'created_at', $sort = 'desc') : LengthAwarePaginator
    {
        return $this->model
            ->orderBy($orderBy, $sort)
            ->paginate($paged);
    }

    /**
     * @param int    $paged
     * @param string $orderBy
     * @param string $sort
     *
     * @return LengthAwarePaginator
     */
    public function getDeletedPaginated($paged = 25, $orderBy = 'created_at', $sort = 'desc') : LengthAwarePaginator
    {
        return $this->model
            ->onlyTrashed()
            ->orderBy($orderBy, $sort)
            ->paginate($paged);
    }

   
    /**
     * @param Quotation  $quotation
     * @param array $data
     *
     * @return Quotation
     * @throws GeneralException
     * @throws \Exception
     * @throws \Throwable
     */
    public function update(Quotation $quotation, array $data) : Quotation
    {
        return DB::transaction(function () use ($quotation, $data) {
            if ($quotation->update([
                'payment_status' => $data['payment_status'],
                'payment_date' => $data['payment_date'],
            ])) {
			     
                event(new QuotationUpdated($quotation));

                return $quotation;
            }

            throw new GeneralException(__('exceptions.backend.access.quotation.update_error'));
        });
    }

    /**
     * @param Quotation $quotation
     *
     * @return Quotation
     * @throws GeneralException
     * @throws \Exception
     * @throws \Throwable
     */
    public function forceDelete(Quotation $quotation) : Quotation
    {
        if (is_null($quotation->deleted_at)) {
            throw new GeneralException(__('exceptions.backend.access.quotation.delete_first'));
        }

        return DB::transaction(function () use ($quotation) {
            // Delete associated relationships

            if ($quotation->forceDelete()) {
                event(new QuotationPermanentlyDeleted($quotation));

                return $quotation;
            }

            throw new GeneralException(__('exceptions.backend.access.quotation.delete_error'));
        });
    }

    /**
     * @param Quotation $quotation
     *
     * @return Quotation
     * @throws GeneralException
     */
    public function restore(Quotation $quotation) : Quotation
    {
        if (is_null($quotation->deleted_at)) {
            throw new GeneralException(__('exceptions.backend.access.quotation.cant_restore'));
        }

        if ($quotation->restore()) {
            event(new QuotationRestored($quotation));

            return $quotation;
        }

        throw new GeneralException(__('exceptions.backend.access.quotation.restore_error'));
    }

    public function quotationDetailsById($quotation_id)
    {
        return $this->model->where('id',$quotation_id)->first();
    }
    

}
