<?php
namespace App\Http\Controllers\API\v1;

use App\Http\Controllers\Controller;
use App\Models\CutoffSetting;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;

class CutoffSettingController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {

        $keyword = $request->input('keyword', null);
        if($keyword) $keyword = str_replace(' ','%', $keyword);
        $perPage = $request->input('per_page', PHP_INT_MAX);
        $sortBy = $request->input('sortBy', null);
        $sortType = $request->input('sortType', null);
        $data = CutoffSetting::with(['company'])
        ->when($keyword, fn($q) => $q->whereHas('company', fn($qc) => $qc->where('name', 'like', '%'.$keyword.'%')))
        ->when($sortBy, fn ($q) => $q->orderBy($sortBy, $sortType))
        ->paginate($perPage);

        return response()->json($data);
    }


    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        // Validate the incoming request data
        $validator = Validator::make($request->all(), [
            'cut_off' => [
                'required',
            ],
            'day_start' => [
                'required',
            ],
            'day_end' => [
                'required',
            ],
            'salary_day' => [
                'required',
            ],
        ]);

        /** If validation fails return with error message */
        if ($validator->fails()) {
            $errors = $validator->errors();
            $first = $errors->getMessages();
            return response()->json([
                'status'  => false,
                'message' => (reset($first))[0],
                'errors'  => $errors
            ], 402);
        }

        DB::connection()->beginTransaction();

        try {

            $data = CutoffSetting::create($request->all());

            DB::connection()->commit();

            return response()->json([
                'message' => 'Record Successfully added!',
                'status' => 'success',
            ],201);

        } catch (\Throwable $e) {
            DB::connection()->rollback();
            return response()->json([
                'status'  => false,
                'message' => 'Unable to process request. Please try again.',
                'data'    => $e->getMessage()
            ]);
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(CutoffSetting $cutoffSetting)
    {
        //
    }

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

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, CutoffSetting $cutoffSetting)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(CutoffSetting $cutoffSetting)
    {
        //
    }
}
