<?php

class ModelPaymentMatera extends Model {
  public function getMethod($address, $total){

    $status = $this->config->get('matera_status');
    $term = $this->config->get('matera_term_status');

    if(!$status || !$term) return false; 

    $this->load->language('payment/matera');
    $this->load->helper('ftr_geral');

    $method_data = [];
    $quote_data = [];

    $matera_pix = $this->config->get('matera_pix');

    if($matera_pix) {

      $pix = ftr_geral::getPaymentMethods([$matera_pix], $total);
      
      if($pix) {
        $quote_data[Type_PaymentOption::Tp_Pix] = [
          'code' => 'matera',
          'code_type' => Type_PaymentOption::Tp_Pix,
          'title' => (!empty(current($pix)['title'])) ? current($pix)['title'] : $this->language->get('text_matera_pix') ,
          'sort_order' => $this->config->get('matera_order')
        ];
      }
    }

    if(empty($quote_data)) return false;
    
    $method_data = [
      'code' => 'matera',
      'quote' => $quote_data,
      'title' => $this->language->get('text_matera_pix'),
      'terms' => $this->config->get('matera_term_status'),
      'sort_order' => $this->config->get('matera_order')
    ];

    return $method_data;
  }

  public function setStatusOrder($order_id, $matera_status_id){

    $this->load->dmodel('checkout/order');

    switch ($matera_status_id){
      case mb_strtoupper(Type_materaPaymentStatus::Tp_Open):
        $order_status_id = $this->config->get('matera_order_status_open');
        break;
      case mb_strtoupper(Type_materaPaymentStatus::Tp_Created):
        $order_status_id = $this->config->get('matera_order_status_created');
        break;
      case mb_strtoupper(Type_materaPaymentStatus::Tp_Approved):
        $order_status_id = $this->config->get('matera_order_status_approved');
        break;
      case mb_strtoupper(Type_materaPaymentStatus::Tp_Rejected):
        $order_status_id = $this->config->get('matera_order_status_rejected');
        break;
      case mb_strtoupper(Type_materaPaymentStatus::Tp_Canceled):
        $order_status_id = $this->config->get('matera_order_status_canceled');
        break;
      case mb_strtoupper(Type_materaPaymentStatus::Tp_Error):
        $order_status_id = $this->config->get('matera_order_status_error');
        break;  
      case mb_strtoupper(Type_materaPaymentStatus::Tp_Partial):
        $order_status_id = $this->config->get('matera_order_status_partial');
        break;
      case mb_strtoupper(Type_materaPaymentStatus::Tp_Overfilled):
        $order_status_id = $this->config->get('matera_order_status_overfilled');
        break;   
      default:
        $order_status_id = $this->config->get('matera_order_status_open');
    }

    $this->load->dmodel('checkout/order');
    $this->dmodel_checkout_order->update($order_id, $order_status_id, '', true);
  }

  public function getStatusPayment($status): bool {
    return match ($status) {
      $this->config->get('matera_order_status_approved') => true,
      default => false,
    };
  }
  
  public function getOrderIdByIntegracao($codigo_integracao){
    $query = $this->db->query("SELECT order_id FROM oc_order WHERE codigo_integracao_pagamento = '$codigo_integracao'")->row;
   
    return (int)$query['order_id'] ?? false;
  }

}
