<?php

namespace App\Repositories\Admin;

use App\Models\Order;
use App\Models\Attachment;
use InfyOm\Generator\Common\BaseRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

/**
 * Class OrderRepository
 * @package App\Repositories\Admin
 * @version February 16, 2022, 6:25 pm UTC
 *
 * @method Order findWithoutFail($id, $columns = ['*'])
 * @method Order find($id, $columns = ['*'])
 * @method Order first($columns = ['*'])
 */
class OrderRepository extends BaseRepository
{
    /**
     * @var array
     */
    protected $fieldSearchable = [
        'production_user_id',
        'lead_id',
        'sales_person_id',
        'production_team_id',
        'link',
        'user_name',
        'password',
        'portal',
        'deadline',
        'comments'
    ];

    /**
     * Configure the Model
     **/
    public function model()
    {
        return Order::class;
    }

    /**
     * @param $request
     * @return mixed
     */
    // public function saveRecord($request)
    // {
    //     $user  = \Auth::user();
    //     $input = $request->all();
    //     $order = $this->create($input);
    //     if ($user->details->is_production == 1) {
    //         $this->handleAttachments($request, $order);
    //     } else {
    //         $this->handleAttachments_for_production($request, $order);
    //     }
    //     return $order;
    // }

    // /**
    //  * @param $request
    //  * @param $order
    //  * @return mixed
    //  */
    // public function updateRecord($request, $order)
    // {
    //     $user  = \Auth::user();
    //     $input = $request->all();
    //     $order = $this->update($input, $order->id);
    //     if ($user->details->is_production == 1) {
    //         $this->handleAttachments($request, $order);
    //     } else {
    //         $this->handleAttachments_for_production($request, $order);
    //     }
    //     return $order;
    // }
    public function saveRecord($request)
    {
        $user  = \Auth::user();
        $input = $request->all();


        if ($user->details->is_production == 1) {
            $order = $this->create($input);
//            $this->handleAttachments_for_production($request, $order);
        } else {
            $input['sales_team_id'] = $user->details->team_id;
            $order                  = $this->create($input);
//            $this->handleAttachments($request, $order);
        }
        $save = app(ThreadRepository::class)->saveRecord2($request, $order->id);
        return $order;
    }

    /**
     * @param $request
     * @param $order
     * @return mixed
     */
    public function updateRecord($request, $order)
    {
        $user  = \Auth::user();
        $input = $request->all();

        if ($user->details->is_production == 1) {
            $order = $this->update($input, $order->id);
            $this->handleAttachments_for_production($request, $order);
        } else {
            $input['sales_team_id'] = $user->details->team_id;
            $order                  = $this->update($input, $order->id);
            $this->handleAttachments($request, $order);
        }
        return $order;
    }

    /**
     * @param $id
     * @return mixed
     */
    public function deleteRecord($id)
    {
        $order = $this->delete($id);
        return $order;
    }

    public function handleAttachments(Request $request, Order $order)
    {

        if ($request->hasFile('attachments')) {

            $files = $request->file('attachments');
            $files = (is_array($files)) ? $files : [$files];
            foreach ($files as $file) {
//                if (strpos($file->getMimeType(), 'image/') !== false) {
                $path = Storage::putFile('production', $file);
                Attachment::create([
                    'path'          => $path,
                    'instance_type' => Attachment::INSTANCE_TYPE_SALES,
                    'instance_id'   => $order->id,
                    'name'          => $file->getClientOriginalName(),
                ]);
//                }

            }
        }
    }

    public function handleAttachments_for_production(Request $request, Order $order)
    {

        if ($request->hasFile('attachments')) {
            $files = $request->file('attachments');
            $files = (is_array($files)) ? $files : [$files];
            foreach ($files as $file) {
//                if (strpos($file->getMimeType(), 'image/') !== false) {
                $path = Storage::putFile('production', $file);
                Attachment::create([
                    'path'          => $path,
                    'instance_type' => Attachment::INSTANCE_TYPE_PRODUCTION,
                    'instance_id'   => $order->id,
                    'name'          => $file->getClientOriginalName(),
                ]);
//                }

            }
        }
    }

}