# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import _, api, fields, models


class ResConfigSettings(models.TransientModel):
    _inherit = 'res.config.settings'

    poynt_surcharge_enabled = fields.Boolean(
        string="Enable Credit Card Surcharge",
        config_parameter='fusion_poynt.surcharge_enabled',
    )
    poynt_surcharge_visa_rate = fields.Float(
        string="Visa Rate (%)",
        config_parameter='fusion_poynt.surcharge_visa_rate',
        default=2.5,
    )
    poynt_surcharge_mastercard_rate = fields.Float(
        string="Mastercard Rate (%)",
        config_parameter='fusion_poynt.surcharge_mastercard_rate',
        default=2.5,
    )
    poynt_surcharge_amex_rate = fields.Float(
        string="Amex Rate (%)",
        config_parameter='fusion_poynt.surcharge_amex_rate',
        default=3.5,
    )
    poynt_surcharge_debit_rate = fields.Float(
        string="Debit Rate (%)",
        config_parameter='fusion_poynt.surcharge_debit_rate',
        default=0.0,
    )
    poynt_surcharge_other_rate = fields.Float(
        string="Other Cards Rate (%)",
        config_parameter='fusion_poynt.surcharge_other_rate',
        default=2.5,
    )
    poynt_surcharge_product_id = fields.Many2one(
        'product.product',
        string="Surcharge Product",
        config_parameter='fusion_poynt.surcharge_product_id',
        help="The service product used for the credit card processing fee line.",
    )

    @api.model
    def get_values(self):
        res = super().get_values()
        ICP = self.env['ir.config_parameter'].sudo()
        product_id = int(ICP.get_param('fusion_poynt.surcharge_product_id', '0') or 0)
        if product_id and self.env['product.product'].sudo().browse(product_id).exists():
            res['poynt_surcharge_product_id'] = product_id
        else:
            default = self.env.ref('fusion_poynt.product_cc_processing_fee', raise_if_not_found=False)
            res['poynt_surcharge_product_id'] = default.id if default else False
        return res

    def set_values(self):
        super().set_values()
        ICP = self.env['ir.config_parameter'].sudo()
        ICP.set_param(
            'fusion_poynt.surcharge_product_id',
            str(self.poynt_surcharge_product_id.id) if self.poynt_surcharge_product_id else '0',
        )

    def action_open_poynt_provider(self):
        provider = self.env['payment.provider'].sudo().search(
            [('code', '=', 'poynt')], limit=1,
        )
        if provider:
            return {
                'type': 'ir.actions.act_window',
                'res_model': 'payment.provider',
                'res_id': provider.id,
                'view_mode': 'form',
                'target': 'current',
            }
        return {
            'type': 'ir.actions.act_window',
            'res_model': 'payment.provider',
            'view_mode': 'list,form',
            'target': 'current',
            'domain': [('code', '=', 'poynt')],
        }

    def action_open_poynt_terminals(self):
        return {
            'type': 'ir.actions.act_window',
            'res_model': 'poynt.terminal',
            'view_mode': 'list,form',
            'target': 'current',
        }
