<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\VaccineMaster;
use Illuminate\Support\Facades\Validator;



class VaccineMasterController extends Controller
{
    // ✅ LIST + SEARCH + PAGINATION
    public function index(Request $request)
    {
        $perPage = $request->get('per_page', 10);
        $search = $request->get('search', '');

        $query = VaccineMaster::query();

        if ($search) {
            $query->where('Vaccine_Name', 'LIKE', "%$search%");
        }

        $data = $query->orderBy('VM_Id', 'DESC')->paginate($perPage);

        return response()->json([
            'status' => true,
            'message' => 'Vaccine list fetched',
            'data' => $data
        ]);
    }


// ALL DATA (No Pagination)
public function all(Request $request)
{
    try {
        $search = $request->get('search', '');

        $query = VaccineMaster::query();

        if ($search) {
            $query->where('Vaccine_Name', 'LIKE', "%$search%");
        }

        $data = $query->orderBy('VM_Id', 'DESC')->get();

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

    } catch (\Exception $e) {

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


    // ✅ STORE
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'Vaccine_Name' => 'required|string|max:100',
            'Validity_Days' => 'required|integer|max:999'
        ]);

        if ($validator->fails()) {
            return response()->json([
                'status' => false,
                'errors' => $validator->errors()
            ]);
        }

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

        return response()->json([
            'status' => true,
            'message' => 'Vaccine created',
            'data' => $data
        ]);
    }

    // ✅ SHOW
    public function show($id)
    {
        $data = VaccineMaster::find($id);

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

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

    // ✅ UPDATE
    public function update(Request $request, $id)
    {
        $data = VaccineMaster::find($id);

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

        $data->update($request->all());

        return response()->json([
            'status' => true,
            'message' => 'Updated successfully',
            'data' => $data
        ]);
    }

    // ✅ DELETE (SOFT DELETE)
    public function destroy($id)
    {
        $data = VaccineMaster::find($id);

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

        $data->delete();

        return response()->json([
            'status' => true,
            'message' => 'Deleted successfully'
        ]);
    }
}
