<?php

namespace App\Repositories;

use Illuminate\Support\Str;
use Storage;

abstract class StorageRepository
{
    protected $model;

    abstract public function get();

    /**
     * Display specified resource.
     *
     * @param  varchar  $with
     * @param  uuid  $id
     * @return \Illuminate\Http\Response
     */
    public function findId($id = null, $with = null)
    {
        return $this->model
            ->when($with, function ($query) use ($with) {
                return $query->with($with);
            })
            ->when($id, function ($query) use ($id) {
                return $query->where('id', $id);
            })
            ->first();
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store($request, $tipe, $is_active = 'on')
    {
        $setting = webprofilesetting();

        $data = $request->except('_token');
        $data['id'] = Str::uuid()->toString();

        if ($request->hasFile($tipe)) {
            $cover = $request->file($tipe);
            $extension = $cover->guessClientExtension();
            $filename = $data['id'] . '.' . $extension;
            if ($setting['external_storage'] == 1) {
                Storage::disk('storage')->put($setting['directory'] . '/' . $tipe . '/' . $filename, file_get_contents($cover->getRealPath()));
            } else {
                Storage::disk('local')->put('public/' . $tipe . '/' . $filename, file_get_contents($cover->getRealPath()));
            }
            $data[$tipe] = $filename;
        }

        if ($is_active == 'on') {
            array_key_exists('is_active', $data) ? $data['is_active'] = 1 : $data['is_active'] = 0;
        }
        $data['userid_created'] = auth()->user()->id;
        
        return $this->model->create($data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  Model  $model
     * @return \Illuminate\Http\Response
     */
    public function update($request, $model, $tipe, $is_active = 'on')
    {
        $setting = webprofilesetting();

        $data = $request->except('_token');

        if ($request->hasFile($tipe)) {
            $cover = $request->file($tipe);
            $extension = $cover->guessClientExtension();
            $filename = $model->id . '.' . $extension;

            if ($setting['external_storage'] == 1) {
                Storage::disk('storage')->put($setting['directory'] . '/' . $tipe . '/' . $filename, file_get_contents($cover->getRealPath()));
            } else {
                Storage::disk('local')->put('public/' . $tipe . '/' . $filename, file_get_contents($cover->getRealPath()));
            }

            $data[$tipe] = $filename;
        }

        if ($is_active == 'on') {
            array_key_exists('is_active', $data) ? $data['is_active'] = 1 : $data['is_active'] = 0;
        }
        $data['userid_updated'] = auth()->user()->id;
        
        return $model->update($data);
    }

    /**
     * Show the specified resource in storage.
     *
     * @param  uuid  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return $this->model->where('user_id', $id)->first();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  Model  $model
     * @return \Illuminate\Http\Response
     */
    public function destroy($model, $tipe)
    {
        $setting = webprofilesetting();

        if ($model->$tipe) {
            if ($setting['external_storage'] == 1) {
                Storage::disk('storage')->delete($setting['directory'] . '/' . $tipe . '/' . $model->$tipe);
            } else {
                Storage::disk('local')->delete('public/' . $tipe . '/' . $model->$tipe);
            }
        }

        $model->delete($model->id);
    }
}
