<?php

namespace App\Http\Controllers;

use App\Models\PetFee;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;

class PetFeeController extends Controller
{
    // LIST + SEARCH + PAGINATION
    public function index(Request $request)
    {
        $search = $request->search;
        $perPage = $request->per_page ?? 10;

        $query = PetFee::with('species');

        if ($search) {
            $query->where(function ($q) use ($search) {
                $q->where('Fee_Type', 'like', "%$search%")
                  ->orWhere('Amount', 'like', "%$search%");
            });
        }

        $data = $query->paginate($perPage);

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


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

        $query = PetFee::with('species');

        if ($search) {
            $query->where(function ($q) use ($search) {
                $q->where('Fee_Type', 'like', "%$search%")
                  ->orWhere('Amount', 'like', "%$search%");
            });
        }

        $data = $query->latest()->get();

        return response()->json([
            'status' => true,
            'message' => 'All Pet Fees 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)
    {
        DB::beginTransaction();

        try {
            $validator = Validator::make($request->all(), [
                'Fee_Type' => 'required|string|max:100',
                'AS_Id' => 'required|exists:animal_species,AS_Id',
                'Sterilized' => 'required|boolean',
                'Amount' => 'required|numeric'
            ]);

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

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

            DB::commit();

            return response()->json([
                'status' => true,
                'message' => 'Pet fee created',
                'data' => $data
            ]);
        } catch (\Exception $e) {
            DB::rollBack();

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

    // SHOW
    public function show($id)
    {
        $data = PetFee::with('species')->find($id);

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

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

    // UPDATE
    public function update(Request $request, $id)
    {
        DB::beginTransaction();

        try {
            $data = PetFee::find($id);

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

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

            DB::commit();

            return response()->json([
                'status' => true,
                'message' => 'Updated successfully',
                'data' => $data
            ]);
        } catch (\Exception $e) {
            DB::rollBack();

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

    // DELETE (Soft Delete)
    public function destroy($id)
    {
        $data = PetFee::find($id);

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

        $data->delete();

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