<?php

namespace App\Http\Controllers\Api\GoPoints;

use App\Http\Controllers\Controller;
use Illuminate\Support\Str;

class InstallmentsController extends Controller
{
    /**
     * Endpoint 4.5 – Consultar opciones de pago a plazos
     * GET /api/gopoints/installments/{campaign}/{value}
     */
    public function show($campaign, $value = null)
    {
        // 1) Validar campaña
        if (Str::lower(trim($campaign)) !== 'gopoints') {
            return response()->json([
                'Status'    => 400,
                'Message'   => 'Campaña no reconocida',
                'CodeError' => 'INVALID_CAMPAIGN',
                'Exception' => null,
                'Parameters'=> [
                    'Campaign' => $campaign,
                    'Amount'   => $value,
                ],
                'PaymentOptions' => [],
            ], 400, [], JSON_UNESCAPED_UNICODE);
        }

        // 2) Validar monto
        if (!is_numeric($value) || floatval($value) <= 0) {
            return response()->json([
                'Status'    => 422,
                'Message'   => 'Monto inválido',
                'CodeError' => 'INVALID_AMOUNT',
                'Exception' => null,
                'Parameters'=> [
                    'Campaign' => $campaign,
                    'Amount'   => $value,
                ],
                'PaymentOptions' => [],
            ], 422, [], JSON_UNESCAPED_UNICODE);
        }

        $amount = (float) $value;

        // 3) Opciones de pago (ejemplo)
        $paymentOptions = [
            [
                'CreditCardType'     => 'MASTERCARD',
                'InstallmentOptions' => [
                    [
                        'Description' => '1 cuota sin interés',
                        'Quotas'      => 1,
                        'QuotaValue'  => $amount,
                    ],
                    [
                        'Description' => '3 cuotas sin interés',
                        'Quotas'      => 3,
                        'QuotaValue'  => round($amount / 3, 2),
                    ],
                    [
                        'Description' => '6 cuotas sin interés',
                        'Quotas'      => 6,
                        'QuotaValue'  => round($amount / 6, 2),
                    ],
                ],
            ],
            [
                'CreditCardType'     => 'VISA',
                'InstallmentOptions' => [
                    [
                        'Description' => '1 cuota sin interés',
                        'Quotas'      => 1,
                        'QuotaValue'  => $amount,
                    ],
                    [
                        'Description' => '12 cuotas con interés',
                        'Quotas'      => 12,
                        'QuotaValue'  => 6100.00,
                    ],
                ],
            ],
        ];

        // 4) Respuesta en el formato oficial
        return response()->json([
            'Status'         => 200,
            'Message'        => '',
            'CodeError'      => '',
            'Exception'      => null,
            'Parameters'     => [
                'Campaign' => 'GoPoints',
                'Amount'   => $amount,
                // 'Currency' => 'CLP', // si el manual lo requiere
            ],
            'PaymentOptions' => $paymentOptions,
        ], 200, [], JSON_UNESCAPED_UNICODE);
    }
}
