<?php

namespace App\Http\Controllers;

use App\Models\Categorie;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use App\Http\Validation\CategorieValidation;

class CategorieController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $categories = Categorie::all();
        return response()->json($categories);
    }

    /**
     * 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, CategorieValidation $validation)
    {
        $validator = Validator::make($request->all(), $validation->rules(), $validation->messages());
        if($validator->fails()){
            return response()->json(['errors' => $validator->errors()],401);
        }

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

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

        $categorie = Categorie::create([
            'name' => $request->input('name'),
            'icon' => $file   
        ]);
        return response()->json($categorie);
    }

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

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

        $request->file('icon')->storeAs('public/icon',$file);
        $icon = $category->icon;
        Storage::delete('public/icon/'.$icon);
        $category->icon = $file;
        $category->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, CategorieValidation $validation)
    {
        $validator = Validator::make($request->all(), $validation->updateRules(), $validation->updateMessages());
        if($validator->fails()){
            return response()->json(['errors' => $validator->errors()],401);
        }

        $category = Categorie::find($id);

        $category->name = $request->get('name');
        $category->save();
    }

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