<?php

namespace App\Http\Controllers\Admin\Color;

use App\Http\Controllers\Controller;
use App\Models\Color;
use App\Models\productAttribute;
use App\Service\Colors\ColorService;
use App\Traits\HasUsageCheck;
use Illuminate\Http\Request;
use Inertia\Inertia;

class ColorController extends Controller
{
    use HasUsageCheck;
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return Inertia::render('Colors/Color');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function view()
    {
        return response()->json(['colors' => Color::orderbydesc('id')->get()]);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
    $request->validate([
            'name' => 'required|unique:colors,name',
        ]);
        ColorService::store(request()->all());

        return response()->json(['success' => 'Color create Successfully', 'colors' => Color::orderbydesc('id')->get()]);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        ColorService::update(request()->all(), $id);

        return response()->json(['success' => 'Color update Successfully', 'colors' => Color::orderbydesc('id')->get()]);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
      
        if ($this->colorCheck($id)) {
            return response()->json(['error' => 'Color already use'], 422);
        }
        Color::where('id', $id)->delete();

        return response()->json(['success' => 'Color Delete Successfully', 'colors' => Color::orderbydesc('id')->get()]);
    }
}
