<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Payment extends Client_Controller
{
    private const GATEWAYS = array(
        'paypal' => array('name' => 'paypal', 'allow' => 'allow_paypal', 'status' => 'paypal_status', 'modal' => true),
        'stripe' => array('name' => 'Stripe', 'allow' => 'allow_stripe', 'status' => 'stripe_status', 'modal' => true),
        'checkout' => array('name' => '2checkout', 'allow' => 'allow_2checkout', 'status' => '2checkout_status', 'modal' => false),
        'authorize' => array('name' => 'Authorize.net', 'allow' => 'allow_authorize_net', 'status' => 'authorize_net_status', 'modal' => false),
        'ccavenue' => array('name' => 'CCAvenue', 'allow' => 'allow_ccavenue', 'status' => 'ccavenue_status', 'modal' => false),
        'braintree' => array('name' => 'Braintree', 'allow' => 'allow_braintree', 'status' => 'braintree_status', 'modal' => false),
        'mollie' => array('name' => 'Mollie', 'allow' => 'allow_mollie', 'status' => 'mollie_status', 'modal' => true),
        'payumoney' => array('name' => 'PayUmoney', 'allow' => 'allow_payumoney', 'status' => 'payumoney_status', 'modal' => false),
        'razorpay' => array('name' => 'Razorpay', 'allow' => 'allow_razorpay', 'status' => 'razorpay_status', 'modal' => true),
        'tappayment' => array('name' => 'TapPayment', 'allow' => 'allow_tappayment', 'status' => 'tappayment_status', 'modal' => true),
    );

    public function __construct()
    {
        parent::__construct();
        $this->load->model('invoice_model');
    }

    public function paypal($action = 'pay', $invoice_id = null)
    {
        return $this->gateway('paypal', $action, $invoice_id);
    }

    public function stripe($action = 'pay', $invoice_id = null)
    {
        return $this->gateway('stripe', $action, $invoice_id);
    }

    public function checkout($action = 'pay', $invoice_id = null)
    {
        return $this->gateway('checkout', $action, $invoice_id);
    }

    public function authorize($action = 'pay', $invoice_id = null)
    {
        return $this->gateway('authorize', $action, $invoice_id);
    }

    public function ccavenue($action = 'pay', $invoice_id = null)
    {
        return $this->gateway('ccavenue', $action, $invoice_id);
    }

    public function braintree($action = 'pay', $invoice_id = null)
    {
        return $this->gateway('braintree', $action, $invoice_id);
    }

    public function mollie($action = 'pay', $invoice_id = null)
    {
        return $this->gateway('mollie', $action, $invoice_id);
    }

    public function payumoney($action = 'pay', $invoice_id = null)
    {
        return $this->gateway('payumoney', $action, $invoice_id);
    }

    public function razorpay($action = 'pay', $invoice_id = null)
    {
        return $this->gateway('razorpay', $action, $invoice_id);
    }

    public function tappayment($action = 'pay', $invoice_id = null)
    {
        return $this->gateway('tappayment', $action, $invoice_id);
    }

    private function gateway($gateway_key, $action = 'pay', $invoice_id = null)
    {
        if ($action !== 'pay') {
            show_404();
        }

        $gateway = self::GATEWAYS[$gateway_key] ?? null;
        if (empty($gateway)) {
            show_404();
        }

        $invoice_id = (int) $invoice_id;
        $invoice_info = $this->invoice_model->check_by(array('invoices_id' => $invoice_id), 'tbl_invoices');
        if (empty($invoice_info) || (int) $invoice_info->client_id !== (int) client_id() || $invoice_info->show_client === 'No') {
            set_message('error', lang('no_data_found'));
            redirect('client/invoice/manage_invoice');
        }

        $allowed_field = $gateway['allow'];
        $status = strtolower((string) config_item($gateway['status']));
        $gateway_allowed = !empty($invoice_info->$allowed_field) && $invoice_info->$allowed_field === 'Yes';
        $gateway_active = $status === 'active';
        $invoice_due = (float) $this->invoice_model->calculate_to('invoice_due', $invoice_id);
        $invoice_due = max(0, $invoice_due);

        $data = array(
            'title' => $gateway['name'] . ' ' . lang('payment'),
            'page_title' => $gateway['name'] . ' ' . lang('payment'),
            'page_description' => $invoice_info->reference_no,
            'gateway_key' => $gateway_key,
            'gateway' => $gateway,
            'invoice_info' => $invoice_info,
            'invoice_due' => $invoice_due,
            'gateway_allowed' => $gateway_allowed,
            'gateway_active' => $gateway_active,
            'is_modal' => !empty($gateway['modal']),
            'client_info' => $this->invoice_model->check_by(array('client_id' => $invoice_info->client_id), 'tbl_client'),
        );

        if ($gateway_key === 'paypal') {
            $data['paypal_url'] = (strtoupper((string) config_item('paypal_live')) === 'FALSE')
                ? 'https://www.sandbox.paypal.com/cgi-bin/webscr'
                : 'https://www.paypal.com/cgi-bin/webscr';
        }

        $data['subview'] = $this->load->view('client/payment/pay', $data, true);
        if ($this->input->is_ajax_request() || !empty($gateway['modal'])) {
            echo $data['subview'];
            return;
        }

        $this->load->view('client/_layout_main', $data);
    }
}
