<?php
/**
 * Stripe payment provider (preparation — guest checkout not yet active).
 *
 * @package Maca_Menu_List
 */

if (!defined('ABSPATH')) {
    exit;
}

require_once MACA_MENULIST_PLUGIN_DIR . 'includes/payments/class-payment-provider.php';

/**
 * Stripe Connect / Checkout preparation.
 */
class Maca_Menulist_Payment_Provider_Stripe extends Maca_Menulist_Payment_Provider {

    /**
     * @return string
     */
    public function get_id() {
        return 'stripe';
    }

    /**
     * @return string
     */
    public function get_label() {
        return __('Stripe', 'maca-restu-pro');
    }

    /**
     * @return string
     */
    public function get_description() {
        return __(
            'Card payments, Apple Pay, Google Pay, and Swish (when enabled in your Stripe account). Recommended first provider.',
            'maca-restu-pro'
        );
    }

    /**
     * @return array<int, string>
     */
    public function get_setting_keys() {
        return array(
            'test_mode',
            'publishable_key',
            'secret_key',
            'webhook_secret',
            'connected_account_id',
        );
    }

    /**
     * @return array<int, string>
     */
    public function get_required_setting_keys() {
        return array('publishable_key', 'secret_key');
    }

    /**
     * @param array<string, string> $values Values.
     * @return true|WP_Error
     */
    public function validate_settings($values) {
        $mode = isset($values['test_mode']) ? $values['test_mode'] : '1';
        $prefix = ($mode === '1') ? 'pk_test_' : 'pk_live_';
        $secret_prefix = ($mode === '1') ? 'sk_test_' : 'sk_live_';

        $pk = isset($values['publishable_key']) ? trim($values['publishable_key']) : '';
        if ($pk !== '' && strpos($pk, $prefix) !== 0) {
            return new WP_Error(
                'maca_stripe_pk',
                sprintf(
                    /* translators: %s: expected key prefix, e.g. pk_test_ */
                    __('Publishable key should start with %s for the selected mode.', 'maca-restu-pro'),
                    $prefix
                )
            );
        }

        $sk = isset($values['secret_key']) ? trim($values['secret_key']) : '';
        if ($sk !== '' && strpos($sk, $secret_prefix) !== 0) {
            return new WP_Error(
                'maca_stripe_sk',
                sprintf(
                    /* translators: %s: expected key prefix, e.g. sk_test_ */
                    __('Secret key should start with %s for the selected mode.', 'maca-restu-pro'),
                    $secret_prefix
                )
            );
        }

        return true;
    }

    /**
     * @return array<int, array<string, mixed>>
     */
    public function get_admin_fields() {
        return array(
            array(
                'key' => 'test_mode',
                'type' => 'checkbox',
                'label' => __('Test mode', 'maca-restu-pro'),
                'description' => __('Use Stripe test keys until you are ready to accept real payments.', 'maca-restu-pro'),
            ),
            array(
                'key' => 'publishable_key',
                'type' => 'text',
                'label' => __('Publishable key', 'maca-restu-pro'),
                'description' => __('Starts with pk_test_ or pk_live_. Safe to use in the browser.', 'maca-restu-pro'),
                'autocomplete' => 'off',
            ),
            array(
                'key' => 'secret_key',
                'type' => 'password',
                'label' => __('Secret key', 'maca-restu-pro'),
                'description' => __('Starts with sk_test_ or sk_live_. Stored encrypted on your server. Leave blank to keep the current key.', 'maca-restu-pro'),
                'autocomplete' => 'new-password',
            ),
            array(
                'key' => 'webhook_secret',
                'type' => 'password',
                'label' => __('Webhook signing secret', 'maca-restu-pro'),
                'description' => __('From Stripe → Developers → Webhooks (whsec_…). Leave blank to keep the current value.', 'maca-restu-pro'),
                'autocomplete' => 'new-password',
            ),
            array(
                'key' => 'connected_account_id',
                'type' => 'text',
                'label' => __('Connected account ID (optional)', 'maca-restu-pro'),
                'description' => __('For Stripe Connect: acct_… on the restaurant’s connected account. Leave empty if you use your own Stripe account keys directly.', 'maca-restu-pro'),
                'autocomplete' => 'off',
            ),
        );
    }
}
