<?php

// phpcs:disable WordPress.WP.I18n.TextDomainMismatch -- Bridge to pay4all-for-twint :
// this WooCommerce integration re-emits the TWINT gateway strings the free
// plugin already ships and translates. Reusing its text domain avoids
// duplicating dozens of msgids × 4 locales in pay4all-pro's .po files.

use Twnt4All\Support\Journal;

if ( !defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

/**
 * Class TwintToken
 * @package Twnt4All\payment\twnt
 */
class TwintToken extends WC_Payment_Token
{
    /** @protected string token type */
    protected $type = 't4a_twint';

    public function validate()
    {
        if ( false === parent::validate() ) {
            return false;
        }
        return true;
    }

    public function get_display_name( $deprecated = '' )
    {
        if ( $desc = $this->get_description() ) {
            /* translators: %1$s dynamic value(s). */
            return sprintf( __( 'TWINT: %1$s', 'pay4all-for-twint' ), $desc );
        }

        return __( 'TWINT', 'pay4all-for-twint' );
    }

    public function get_description()
    {
        return $this->get_meta( 'description' );
    }

    public function set_description( $description )
    {
        $this->add_meta_data( 'description', $description, true );
    }

    public function get_expy()
    {
        return $this->get_meta( 'expy' );
    }

    public function set_expy( $expy )
    {
        $this->add_meta_data( 'expy', $expy, true );
    }

    public function get_expm()
    {
        return $this->get_meta( 'expm' );
    }

    public function set_expm( $expm )
    {
        $this->add_meta_data( 'expm', $expm, true );
    }

    public function get_card_type()
    {
        return $this->get_meta( 'card_type' );
    }

    public function set_card_type( $card_type )
    {
        $this->add_meta_data( 'card_type', $card_type );
    }

    public static function save_token( $gateway_id, $alias, $user_id, $data, $set_users_default = false )
    {
        $token = new static();
        $token->set_token( $alias ); // Token comes from payment processor
        $token->set_gateway_id( $gateway_id );
        $token->set_user_id( $user_id );

        if ( isset( $data[ 'exp_month' ] ) ) {
            $token->set_expm( $data[ 'exp_month' ] );
        }

        if ( isset( $data[ 'exp_year' ] ) ) {
            $token->set_expy( $data[ 'exp_year' ] );
        }

        if ( isset( $data[ 'description' ] ) ) {
            $token->set_description( $data[ 'description' ] );
        }

        $token->set_card_type( __( 'TWINT', 'pay4all-for-twint' ) );

        if ( !$token->save() ) {
            Journal::error( sprintf( 'Token for user %1$s could not be saved', $user_id ) );
            return;
        }
        Journal::event( sprintf( 'Created new token %1$s for user %2$s.', $token->get_id(), $user_id ) );

        if ( $set_users_default ) {
            \WC_Payment_Tokens::set_users_default( $user_id, $token->get_id() );
        }
    }
}
