<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Designation;
use Illuminate\Support\Facades\DB;

class DesignationController extends Controller
{
    /**
     * List all resources.
     */
    public function index(Request $request)
    {
        $data = Designation::
            ->whereNull('deleted_at')
            ->get();

        return response()->json([
            'success' => true,
            'data' => $data,
        ], 200);
    }

    /**
     * Store a newly created resource.
     */
    public function store(Request $request)
    {
        $validator = \Validator::make($request->all(), [
            'designation' => 'required|string|max:255'
        ]);

        if ($validator->fails()) {
            return response()->json([
                'success' => false,
                'message' => 'Validation failed',
                'errors' => $validator->errors(),
            ], 422);
        }

        $table = new Designation();

            $table->designation = $request->input('designation');

        $table->save();
        $max_id = $table->id;




        return response()->json([
            'success' => true,
            'message' => 'Record created successfully',
            'data' => ['id' => $max_id],
        ], 201);
    }

    /**
     * Display the specified resource.
     */
    public function show(Request $request, $id)
    {
        $row = Designation::whereNull('deleted_at')->find($id);

        if (!$row) {
            return response()->json([
                'success' => false,
                'message' => 'Record not found',
            ], 404);
        }

        return response()->json([
            'success' => true,
            'data' => $row,
        ], 200);
    }

    /**
     * Update the specified resource.
     */
    public function update(Request $request, $id)
    {
        $table = Designation::whereNull('deleted_at')->find($id);

        if (!$table) {
            return response()->json([
                'success' => false,
                'message' => 'Record not found',
            ], 404);
        }

        $validator = \Validator::make($request->all(), [
            'designation' => 'nullable|string|max:255'
        ]);

        if ($validator->fails()) {
            return response()->json([
                'success' => false,
                'message' => 'Validation failed',
                'errors' => $validator->errors(),
            ], 422);
        }


            $table->designation = $request->input('designation');

        $table->update();





        return response()->json([
            'success' => true,
            'message' => 'Record updated successfully',
            'data' => ['id' => (int) $id],
        ], 200);
    }

    /**
     * Remove the specified resource.
     */
    public function destroy(Request $request, $id)
    {
        $row = Designation::whereNull('deleted_at')->find($id);

        if (!$row) {
            return response()->json([
                'success' => false,
                'message' => 'Record not found',
            ], 404);
        }

        $row->delete();

        return response()->json([
            'success' => true,
            'message' => 'Record deleted successfully',
        ], 200);
    }
}
