<?php

namespace App\Http\Controllers\Api;

use app\Http\Requests\StoreCustomerPaymentRequest;
use app\Http\Requests\UpdateCustomerPaymentRequest;
use app\Http\Resources\CustomerPaymentResource;
use App\Models\CustomerPayment;
use Illuminate\Http\Request;

class CustomerPaymentController extends BaseApiController
{
    public function index()
    {
        $payments = CustomerPayment::with(['booking', 'customer'])->latest()->paginate(10);
        return $this->success($payments, 'Customer payments retrieved successfully.');
    }

    public function store(StoreCustomerPaymentRequest $request)
    {
        $payment = CustomerPayment::create($request->validated());
        return $this->success(new CustomerPaymentResource($payment), 'Customer payment created successfully.', 201);
    }

    public function show(CustomerPayment $customerPayment)
    {
        $customerPayment->load(['booking', 'customer']);
        return $this->success(new CustomerPaymentResource($customerPayment), 'Customer payment retrieved successfully.');
    }

    public function update(UpdateCustomerPaymentRequest $request, CustomerPayment $customerPayment)
    {
        $customerPayment->update($request->validated());
        $customerPayment->load(['booking', 'customer']);
        return $this->success(new CustomerPaymentResource($customerPayment), 'Customer payment updated successfully.');
    }

    public function destroy(CustomerPayment $customerPayment)
    {
        $customerPayment->delete();
        return response()->json(null, 204);
    }
}