<?php

namespace App\Http\Controllers;

use App\Enums\PaymentStatusEnum;
use App\Http\Requests\PaymentRequest;
use App\Http\Resources\PaymentResource;
use App\Models\Schedule;
use App\Services\PaymentService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PaymentController extends Controller
{
    public function __construct(
        protected PaymentService $service,
    ) {}

    public function index(): JsonResponse
    {
        $items = $this->service->getAll();

        return $this->successResponse(payload: PaymentResource::collection($items));
    }

    public function store(PaymentRequest $request): JsonResponse
    {
        $item = $this->service->create($request->validated());

        return $this->successResponse(
            payload: new PaymentResource($item),
            message: $this->paymentMessage($item->status->value),
            code: 201,
        );
    }

    public function paySchedule(Request $request, Schedule $schedule): JsonResponse
    {
        $validated = $request->validate([
            'payment_method'           => ['required', 'in:credit_card,pix'],
            'client_payment_method_id' => ['nullable', 'integer', 'exists:client_payment_methods,id'],
            'card_id'                  => ['nullable', 'string', 'max:255'],
            'phone'                    => ['nullable', 'string', 'max:20'],
        ]);

        if ($schedule->client?->user_id !== Auth::id()) {
            abort(403);
        }

        $item = $this->service->payAcceptedSchedule(
            schedule: $schedule,
            paymentMethod: $validated['payment_method'],
            clientPaymentMethodId: $validated['client_payment_method_id'] ?? null,
            options: [
                'phone'   => $validated['phone'] ?? null,
                'card_id' => $validated['card_id'] ?? null,
            ],
        );

        if ($item->status === PaymentStatusEnum::FAILED) {
            return response()->json([
                'payload' => new PaymentResource($item),
                'message' => $item->failure_message ?: $this->paymentMessage($item->status->value),
            ], 422);
        }

        return $this->successResponse(
            payload: new PaymentResource($item),
            message: $this->paymentMessage($item->status->value),
            code: 201,
        );
    }

    public function show(int $id): JsonResponse
    {
        $item = $this->service->findById($id);

        return $this->successResponse(payload: new PaymentResource($item));
    }

    public function update(PaymentRequest $request, int $id): JsonResponse
    {
        $item = $this->service->update($id, $request->validated());

        return $this->successResponse(payload: new PaymentResource($item), message: __('messages.updated'));
    }

    public function destroy(int $id): JsonResponse
    {
        $this->service->delete($id);

        return $this->successResponse(message: __('messages.deleted'), code: 204);
    }

    private function paymentMessage(string $status): string
    {
        return match ($status) {
            'paid' => __('messages.payment_confirmed'),
            'authorized', 'processing', 'pending' => __('messages.payment_pending_confirmation'),
            'failed' => __('messages.payment_not_confirmed'),
            default  => __('messages.created'),
        };
    }
}
