<?php
class ModelPaymentPixpdv extends Model {
	
	public function getFullAccountDB() {
		$sql = "SELECT * FROM oc_pix_config";

		$query = $this->db->query($sql);

		return $query->row;
	}
  public function getMethod($address, $total){

    $status = $this->config->get('pixpdv_status');

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

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

    $pixpdv_data = $this->config->get('pixpdv_pix');

    if($pixpdv_data) {

      $pix = ftr_geral::getPaymentMethods([$pixpdv_data], $total);
      
      if($pix) {
        $quote_data[Type_PaymentOption::Tp_Pix] = [
          'code' => 'pixpdv',
          '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('pixpdv_order')
        ];
      }
    }

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

    return $method_data;
  }

  public function getStatusPayment($status): bool {
    return match ($status) {
      $this->config->get('pixpdv_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;
  }

  public function setStatusOrder($order_id, $pixpdv_status_id){

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

    switch ($pixpdv_status_id) {
      case mb_strtoupper(Type_pixpdvPaymentStatus::Tp_Open):
        $order_status_id = $this->config->get('pixpdv_order_status_open');
        break;
      case mb_strtoupper(Type_pixpdvPaymentStatus::Tp_Created):
        $order_status_id = $this->config->get('pixpdv_order_status_created');
        break;
      case mb_strtoupper(Type_pixpdvPaymentStatus::Tp_Approved):
      case mb_strtoupper(Type_pixpdvPaymentStatus::Tp_Authorized):
        $order_status_id = $this->config->get('pixpdv_order_status_approved');
        break;
      case mb_strtoupper(Type_pixpdvPaymentStatus::Tp_Rejected):
        $order_status_id = $this->config->get('pixpdv_order_status_rejected');
        break;
      case mb_strtoupper(Type_pixpdvPaymentStatus::Tp_Canceled):
      case mb_strtoupper(Type_pixpdvPaymentStatus::Tp_Canceling):
      case mb_strtoupper(Type_pixpdvPaymentStatus::Tp_Expired):
        $order_status_id = $this->config->get('pixpdv_order_status_canceled');
        break;
      case mb_strtoupper(Type_pixpdvPaymentStatus::Tp_Error):
        $order_status_id = $this->config->get('pixpdv_order_status_error');
        break;  
      case mb_strtoupper(Type_pixpdvPaymentStatus::Tp_Partial):
        $order_status_id = $this->config->get('pixpdv_order_status_partial');
        break;
      case mb_strtoupper(Type_pixpdvPaymentStatus::Tp_Overfilled):
        $order_status_id = $this->config->get('pixpdv_order_status_overfilled');
        break;   
      default:
        $order_status_id = $this->config->get('pixpdv_order_status_open');
    }
    
    $this->load->dmodel('checkout/order');
    $this->dmodel_checkout_order->update($order_id, $order_status_id, '', true);
  }

  public function getLastQueryPixStatus($codigo_integracao) {
    $query = $this->db->query("SELECT * FROM oc_payment_pix WHERE payment_id = '$codigo_integracao' LIMIT 1");
    
    if ($query->num_rows) {
      return $query->row;
    }
    
    return false;
  }

}