<?php

namespace App\Http\Controllers;

use App\Models\PetCertificate;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Validator;

class PetCertificateController extends Controller
{
    // LIST (Pagination + Search)
    public function index(Request $request): JsonResponse
    {
        try {
            $perPage = $request->get('per_page', 10);
            $search = $request->get('search', '');

            $query = PetCertificate::with(['pet', 'certificateMaster']);

            if (!empty($search)) {
                $query->where(function ($q) use ($search) {
                    $q->where('Certificate_No', 'like', "%$search%")
                      ->orWhere('Reference_Id', 'like', "%$search%");
                });
            }

            $data = $query->latest('PC_Id')->paginate($perPage);

            return response()->json([
                'status' => true,
                'message' => 'Pet Certificates List',
                'data' => $data
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'status' => false,
                'message' => 'Something went wrong. Please try again later.'
            ], 500);
        }
    }

        public function all(): JsonResponse
    {
        $data = PetCertificate::with(['pet', 'certificateMaster'])
                    ->latest('PC_Id')
                    ->get();

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

    // STORE
    public function store(Request $request): JsonResponse
    {
        $validator = Validator::make($request->all(), [
            'PR_Id' => 'required|exists:pet_registration,PR_Id',
            'CM_Id' => 'required|exists:certificate_master,CM_Id',
            'Certificate_No' => 'required|string|max:50',
            'Issue_Date' => 'required|date',
            'Expiry_Date' => 'required|date|after_or_equal:Issue_Date',
        ]);

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

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

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

    // SHOW
    public function show($id): JsonResponse
    {
        $data = PetCertificate::with(['pet', 'certificateMaster'])
                    ->find($id);

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

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

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

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

        $validator = Validator::make($request->all(), [
            'PR_Id' => 'required|exists:pet_registration,PR_Id',
            'CM_Id' => 'required|exists:certificate_master,CM_Id',
            'Certificate_No' => 'required|string|max:50',
            'Issue_Date' => 'required|date',
            'Expiry_Date' => 'required|date|after_or_equal:Issue_Date',
        ]);

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

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

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

    // DELETE
    public function destroy($id): JsonResponse
    {
        $data = PetCertificate::find($id);

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

        $data->delete();

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