<?php

namespace App\Repositories\Frontend;

use Carbon\Carbon;
use App\Models\QuotationToImage;
use Illuminate\Support\Facades\DB;
use App\Exceptions\GeneralException;
use App\Repositories\BaseRepository;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;

/**
 * Class ServiceProviderCityRepository.
 */
class QuotationToImagesRepository extends BaseRepository
{
    /**
     * @return string
     */
    public function model()
    {
        return QuotationToImage::class;
    }

    /**
     * @param int $quotation_id
     * @param string $orderBy
     * @param string $sort
     *
     * @return mixed
     */
    public function getAll($quotation_id = null)
    {
        return $this->model
            ->where('quotation_id', $quotation_id)
            ->orderBy('id', 'asc')
            ->get();
    }

    /**
     * @param array $data
     *
     * @return QuotationToImages
     * @throws \Exception
     * @throws \Throwable
     */
    public function create(array $data)
    {
        
        
        return DB::transaction(function () use ($data) {
            $image_location = null;
            $name = '';
            if ($data['image_location']) {
                $name = time().str_random(5).'.'.$data['image_location']->getClientOriginalExtension();
                $path = 'quotation';
                $destination = public_path('storage/quotation');
                $avatar_location = $path.'/'.$name;
                $img = Image::make($image->getRealPath());
                $img->resize(260, 260, function ($constraint) {
                    $constraint->aspectRatio();
                })->save($destination.'/'.$name);
            }
            $quotationtoImages = parent::create([
                'quotation_id' => $data['quotation_id'],
                'image_location' => 'quotation/' . $name ,
            ]);

            if ($quotationtoImages) {
                return $quotationtoImages;
            }

            //throw new GeneralException(__('exceptions.backend.access.city.create_error'));
        });
    }
	
	
	
	
}
