<?php

namespace App\Repositories\Admin;

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

/**
 * Class ThreadRepository
 * @package App\Repositories\Admin
 * @version January 11, 2023, 5:30 am PST
 *
 * @method Thread findWithoutFail($id, $columns = ['*'])
 * @method Thread find($id, $columns = ['*'])
 * @method Thread first($columns = ['*'])
 */
class ThreadRepository extends BaseRepository
{
    /**
     * @var array
     */
    protected $fieldSearchable = [
        'order_id',
        'sales_user_id',
        'production_user_id',
        'comments',
        'attachment'
    ];

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

    /**
     * @param $request
     * @return mixed
     */
    public function saveRecord($request)
    {
        $input = $request->only(['comments', 'order_id']);
        $user  = \Auth::user();
        if (\Auth::user()->details->is_production == 0) {
            $input['sales_user_id']      = $user->id;
            $input['production_user_id'] = null;
            $thread                      = $this->create($input);
            $this->handleAttachments($request, $thread);

        } elseif (\Auth::user()->details->is_production == 1) {
            $input['sales_user_id']      = null;
            $input['production_user_id'] = $user->id;
            $thread                      = $this->create($input);
            $this->handleAttachments_for_production($request, $thread);

        } else {
            $input['sales_user_id']      = null;
            $input['production_user_id'] = null;
            $thread                      = $this->create($input);
            $this->handleAttachments_for_Admin($request, $thread);
        }


        return $thread;
    }

    public function saveRecord2($request, $order_id)
    {

        $input             = $request->only(['comments', 'attachments', 'order_id']);

        $input['order_id'] = $order_id;
        unset($input['attachments']);
        $user              = \Auth::user();
        if (\Auth::user()->details->is_production == 0) {
            $input['sales_user_id']      = $user->id;
            $input['production_user_id'] = null;

            $thread                      = $this->create($input);

            $this->handleAttachments($request, $thread);

        } elseif (\Auth::user()->details->is_production == 1) {
            $input['sales_user_id']      = null;
            $input['production_user_id'] = $user->id;
            $thread                      = $this->create($input);
            $this->handleAttachments_for_production($request, $thread);

        } else {
            $input['sales_user_id']      = null;
            $input['production_user_id'] = null;
            $thread                      = $this->create($input);
            $this->handleAttachments_for_Admin($request, $thread);
        }


        return $thread;
    }

    /**
     * @param $request
     * @param $thread
     * @return mixed
     */
    public function updateRecord($request, $thread)
    {
        $input  = $request->all();
        $thread = $this->update($input, $thread->id);
        return $thread;
    }

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

    public function handleAttachments(Request $request, Thread $thread)
    {

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

            $files = $request->file('attachments');
            $files = (is_array($files)) ? $files : [$files];
//            dd($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'   => $thread->id,
                    'name'          => $file->getClientOriginalName(),
                ]);
//                }

            }
        }
    }

    public function handleAttachments_for_production(Request $request, Thread $thread)
    {

        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'   => $thread->id,
                    'name'          => $file->getClientOriginalName(),
                ]);
//                }

            }
        }
    }

    public function handleAttachments_for_Admin(Request $request, Thread $thread)
    {
        dd('ddfd');
        dd('ddfd');

        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_ADMIN,
                    'instance_id'   => $thread->id,
                    'name'          => $file->getClientOriginalName(),
                ]);
//                }

            }
        }
    }

}
