<?php

namespace Modules\Inventory\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Modules\Core\Helpers\ApiResponse;
use Modules\Inventory\DTO\PurchasePaymentAccountFilter;
use Modules\Inventory\Http\Requests\Purchase\PostPurchasePaymentRequest;
use Modules\Inventory\Http\Requests\Purchase\ReversePurchasePaymentRequest;
use Modules\Inventory\Http\Requests\Purchase\StorePurchasePaymentRequest;
use Modules\Inventory\Http\Requests\Purchase\SubmitPurchasePaymentRequest;
use Modules\Inventory\Http\Requests\Purchase\UpdatePurchasePaymentRequest;
use Modules\Inventory\Models\PurchaseInvoice;
use Modules\Inventory\Models\PurchasePayment;
use Modules\Inventory\Repositories\PurchasePaymentRepository;
use Modules\Inventory\Repositories\PurchaseRepository;
use Modules\Inventory\Services\PurchasePaymentService;
use Modules\Inventory\Transformers\Purchase\PurchasePaymentIndexResource;
use Modules\Inventory\Transformers\Purchase\PurchasePaymentResource;

class PurchasePaymentController extends Controller
{
    use AuthorizesRequests;

    public function __construct(
        protected PurchasePaymentService $paymentService,
        protected PurchaseRepository $purchaseRepo,
        private PurchasePaymentRepository $paymentRepo,
    ) {}

    public function searchPurchasePaymentAccounts(Request $request)
    {
        $filters = PurchasePaymentAccountFilter::fromRequest($request);

        $accounts = $this->paymentService->getAccountsForPayment($filters);

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

    public function index0()
    {
        $payments = $this->paymentService->list();

        return PurchasePaymentResource::collection(
            $payments
        )->additional([
            'status' => true,
            'message' => 'Payments retrieved successfully.'
        ]);
    }

    public function index(PurchaseInvoice $purchase)
    {
        $this->authorize('viewAny', PurchasePayment::class);

        $payments = $this->paymentService->getByInvoice($purchase);

        return PurchasePaymentIndexResource::collection($payments)->additional([
            'status' => true,
            'message' => 'Purchase payments retrieved successfully.',
        ]);
    }

    public function paymentSummary(PurchaseInvoice $purchase)
    {
        $this->authorize('create', PurchasePayment::class);

        $summary = $this->paymentService->paymentSummary($purchase);
        
        return ApiResponse::successResponse(
            message: 'Payment summary retrieved successfully.',
            data: $summary
        );
    }

    public function store(StorePurchasePaymentRequest $request, int $id)
    {
        $this->authorize('create', PurchasePayment::class);

        $invoice = $this->purchaseRepo->findForUpdate($id);

        $payment = $this->paymentService->store(
            $invoice,
            $request->validated()
        );

        return (new PurchasePaymentResource($payment))->additional([
            'status' => true,
            'message' => 'Purchase payment successfully recorded.'
        ]);
    }

    public function show(PurchaseInvoice $purchase, PurchasePayment $payment)
    {
        $this->authorize('view', $payment);

        $payment = $this->paymentService->show($payment);

        return (new PurchasePaymentResource($payment))->additional([
            'status' => true,
            'message' => 'Payment retrieved successfully.'
        ]);
    }

    public function update(UpdatePurchasePaymentRequest $request, PurchaseInvoice $purchase, PurchasePayment $payment) {
        $this->authorize('update', $payment);

        $payment = $this->paymentService
            ->update(
                payment: $payment,
                data: $request->validated()
            );

        return (new PurchasePaymentResource($payment))
            ->additional([
                'status' => true,
                'message' => 'Payment updated successfully.',
            ]);
    }

    public function submit(SubmitPurchasePaymentRequest $request, PurchaseInvoice $purchase, PurchasePayment $payment)
    {
        $this->authorize('update', $payment);

        $invoice = $this->paymentService->submit($payment);

        return (new PurchasePaymentResource($invoice))->additional([
            'status' => true,
            'message' => 'Payment submitted successfully.'
        ]);
    }

    public function post(PostPurchasePaymentRequest $request, PurchaseInvoice $purchase, PurchasePayment $payment)
    {
        $this->authorize('post', $payment);

        $payment = $this->paymentService->post($payment);

        return (new PurchasePaymentResource($payment))->additional([
            'status' => true,
            'message' => 'Payment posted successfully.',
        ]);
    }

    public function reverse(ReversePurchasePaymentRequest $request, PurchaseInvoice $purchase, PurchasePayment $payment)
    {
        $payment = $this->paymentService->reverse($payment, $request->validated());

        return (new PurchasePaymentResource($payment))->additional([
            'success' => true,
            'message' => 'Payment reversed successfully.',
        ]);
    }

    public function destroy(PurchaseInvoice $purchase, PurchasePayment $payment)
    {
        $this->paymentService->delete($payment);

        return ApiResponse::successResponse('Payment deleted successfully.');
    }
}
