<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Product;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * 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)
    {
        $request->validate([
            'name' => 'required',
            'description' => 'required'
        ]);

        $category = new Category;
        $category->name = $request->name;
        $category->description = $request->description;
        $category->save();
        
        return response()->json([
            'status' => 'success',
            'message' => 'category added successfully!'
        ], 200);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function show(Category $category)
    {
        $categories = $category->all();
        $products = Product::all();

        return response()->json(
            ['status' => 'success',
            'message' => 'successful',
            'categories' => $categories,
            'products' => $products
            ],200);
    }

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Category $category)
    {
        $request->validate([
            'name' => 'required',
            'id' => 'required'
        ]);

        $category = $category->find($request->id);
        if (!$category) {
            return response()->json([
                'status' => 'error',
                'message' => 'invalid request'
            ], 400);
        }

        $category->name = $request->name;
        $category->save();
        return response()->json([
            'status' => 'success',
            'message' => 'category updated successfully!'
        ], 200);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request, Category $category)
    {
        $request->validate([
            'id' => 'required'
        ]);

        $category = $category->find($request->id);
        if (!$category) {
            return response()->json([
                'status' => 'error',
                'message' => 'invalid request'
            ], 400);
        }

        $category->delete();
        return response()->json([
            'status' => 'success',
            'message' => 'category deleted successfully!'
        ], 200);
    }
}
