<?php

namespace App\Http\Controllers;

use App\Models\Article;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use App\Http\Validation\ArticleValidation;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function search(Request $request)
    {
        $param = $request->input('search');
        $articles = Article::where('name','like', '%'.$param.'%')->get();
        
        return response()->json($articles);
    }

    public function getArticles(Request $request)
    {
        $sortType = $request->input('sortType');
        $articles = Article::orderBy($sortType)->get();
        
        return response()->json($articles);
    }

    public function index(Request $request)
    {
        $articles = Article::all();
        
        return response()->json($articles);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, ArticleValidation $validation) {
        
        $validator = Validator::make($request->all(), $validation->rules(), $validation->messages());
        if($validator->fails()){
            return response()->json(['errors' => $validator->errors()],401);
        }

        $fullFileName = $request->file('image')->getClientOriginalName();
        $fileName = pathinfo($fullFileName, PATHINFO_FILENAME);
        $extension = $request->file('image')->getClientOriginalExtension();
        $file = $fileName.'_'.time().'.'.$extension;

        $request->file('image')->storeAs('public/img',$file);

        $article = Article::create([
            'name' => $request->input('name'),
            'description' => $request->input('description'),
            'quantity' => $request->input('quantity'),
            'price' => $request->input('price'),
            'code' => $request->input('code'),
            'image' => $file,
            'category_id' => $request->input('category_id')
        ]);
        return response()->json($article);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function updateImage(Request $request, $id) {
        $article = Article::find($id);

        $fullFileName = $request->file('image')->getClientOriginalName();
        $fileName = pathinfo($fullFileName, PATHINFO_FILENAME);
        $extension = $request->file('image')->getClientOriginalExtension();
        $file = $fileName.'_'.time().'.'.$extension;

        $request->file('image')->storeAs('public/img',$file);
        $img = $article->image;
        Storage::delete('public/img/'.$img);
        $article->image = $file;
        $article->save();

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id, ArticleValidation $validation)
    {
        $validator = Validator::make($request->all(), $validation->updateRules(), $validation->updateMessages());
        if($validator->fails()){
            return response()->json(['errors' => $validator->errors()],401);
        }

        $article = Article::find($id);

        $article->name =  $request->get('name');
        $article->description = $request->get('description');
        $article->quantity = $request->get('quantity');
        $article->price = $request->get('price');
        $article->code = $request->get('code');
        $article->category_id = $request->get('category_id');
        $article->save();

     }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $article = Article::find($id);
        $img = $article->image;
        Storage::delete('public/img/'.$img);
        $article->delete();
        
    }
}
