<?php

namespace App\Http\Controllers;

use App\Models\CertificateMaster;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class CertificateMasterController extends Controller
{
    
    public function index(Request $request)
    {
        $search = $request->search;
        $perPage = $request->per_page ?? 10;

        $query = CertificateMaster::query();

        if ($search) {
        $query->where('Certificate_Type', 'like', "%$search%")
              ->orWhereDate('Validity', $search); 
    }

        $data = $query->orderBy('CM_Id', 'desc')->paginate($perPage);

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



public function all(Request $request)
{
    try {
        $search = $request->search;

        $query = CertificateMaster::query();

        if ($search) {
            $query->where(function ($q) use ($search) {
                $q->where('Certificate_Type', 'like', "%$search%")
                  ->orWhereDate('Validity', $search);
            });
        }

        $data = $query->orderBy('CM_Id', 'desc')->get();

        return response()->json([
            'status' => true,
            'message' => 'All Certificates Data',
            'data' => $data
        ]);

    } catch (\Exception $e) {

        return response()->json([
            'status' => false,
            'message' => 'Something went wrong. Please try again later.'
        ], 500);
    }
}


    
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'Certificate_Type' => 'required|string|max:100',
            'Validity' => 'nullable|date'
        ]);

        if ($validator->fails()) {
            return response()->json($validator->errors(), 422);
        }

           $data = CertificateMaster::create([
        'Certificate_Type' => $request->Certificate_Type,
        'Validity' => $request->Validity 
    ]);


        return response()->json([
            'message' => 'Certificate Created',
            'data' => $data
        ]);
    }

    
    public function show($id)
    {
        $data = CertificateMaster::find($id);

        if (!$data) {
            return response()->json(['message' => 'Not Found'], 404);
        }

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

    
public function update(Request $request, $id)
{
    $data = CertificateMaster::where('CM_Id', $id)->first();


    if (!$data) {
        return response()->json(['message' => 'Not Found'], 404);
    }

    $validator = Validator::make($request->all(), [
        'Certificate_Type' => 'nullable|string|max:100',
        'Validity' => 'nullable|date'
    ]);

    if ($validator->fails()) {
        return response()->json($validator->errors(), 422);
    }

    $data->update([
        'Certificate_Type' => $request->Certificate_Type ?? $data->Certificate_Type,
        'Validity' => $request->Validity ?? $data->Validity
    ]);

    return response()->json([
        'message' => 'Updated Successfully',
        'data' => $data
    ]);
}
    
    public function destroy($id)
    {
        $data = CertificateMaster::find($id);

        if (!$data) {
            return response()->json(['message' => 'Not Found'], 404);
        }

        $data->delete();

        return response()->json([
            'message' => 'Deleted Successfully'
        ]);
    }
}
