<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreAppointmentCategoryRequest;
use App\Http\Requests\UpdateAppointmentCategoryRequest;
use App\Http\Resources\AppointmentCategoryResource;
use App\Models\AppointmentCategory;
use Illuminate\Http\Request;

class AppointmentCategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return AppointmentCategoryResource::collection(AppointmentCategory::all());
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreAppointmentCategoryRequest $request)
    {
        $fields = $request->validated();

        $category = AppointmentCategory::create($fields);

        return response()->json([
            "category" => new AppointmentCategoryResource($category)
        ], 201);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        $category = AppointmentCategory::find($id);

        if ($category) {
            return new AppointmentCategoryResource($category);
        } else {
            return response()->json(['message' => 'Category not found'], 404);
        }
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateAppointmentCategoryRequest $request, $id)
    {
        $category = AppointmentCategory::find($id);

        if (!$category) {
            return response()->json(['message' => 'Category not found'], 404);
        }

        $fields = $request->validated();
        $category->update($fields);

        return response()->json(["category" => new AppointmentCategoryResource($category)], 200);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        $category = AppointmentCategory::find($id);

        if (!$category) {
            return response()->json(['message' => 'Category not found'], 404);
        }

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

    public function activate($id) {
        $category = AppointmentCategory::find($id);

        if (!$category) {
            return response()->json(['message' => 'Category not found'], 404);
        }

        $category->update(['status' => 'active']);

        return response()->json(["category" => new AppointmentCategoryResource($category)], 200);
    }

    public function deactivate($id) {
        $category = AppointmentCategory::find($id);

        if (!$category) {
            return response()->json(['message' => 'Category not found'], 404);
        }

        $category->update(['status' => 'inactive']);

        return response()->json(["category" => new AppointmentCategoryResource($category)], 200);
    }
}
