<?php

defined('BASEPATH') or exit('No direct script access allowed');
class Mailtest extends MY_Controller
{

    public $receiver_email;
    public $use_resend = true; // Variable pour choisir la config

    public function __construct()
    {
        parent::__construct();
        $this->receiver_email = 'andy.chal@laposte.net';
    }

    public function smtp_config()
    {
        if ($this->use_resend) {
            $config = array(
                'protocol' => 'smtp',
                'smtp_host' => 'smtp.resend.com',
                'smtp_port' => 587, // Using STARTTLS port
                'smtp_user' => 'resend',
                'smtp_pass' => 're_Qr7Mytfo_LCMADh1hcqVXL9V9G5yhwJy5',
                'smtp_crypto' => 'tls', // Enable TLS encryption
                'mailtype' => 'html',
                'from_email' => 'noreply@internet-libre.org', 
                'from_name' => 'Ygg',
                'newline' => "\r\n",
                'crlf' => "\r\n",
                'wordwrap' => TRUE
            );
        } else {
            $config = array(
                'protocol' => 'smtp',
                'smtp_host' => 'smtp.elasticemail.com',
                'smtp_port' => 25,
                'smtp_user' => 'noreply@yggtorrent.wtf',
                'smtp_pass' => '155BD5F5311D1D3B3539EB7EF7EFC2538B89',
                'mailtype' => 'html',
                'from_email' => 'noreply@yggtorrent.qa',
                'from_name' => 'Ygg',
                'newline' => "\r\n",
                'crlf' => "\r\n",
                'wordwrap' => TRUE
            );
        }
        return $config;
    }

    public function reset()
    {
        $token = '1234567890';
        $config = $this->smtp_config();
        $this->load->library('email', $config);
        $this->email->from($config['from_email'], $config['from_name']);
        $this->email->subject('Mot de passe oublié');
        $url = 'https://www.ygg.re/user/reset_password?action=proceed&token=' . $token;
        
        // Message avec uniquement des balises br et a
        $msg .= "Cliquez sur le lien ci-dessous pour réinitialiser votre mot de passe :<br><br>";
        $msg .= "<a href=\"" . $url . "\">Réinitialiser mon mot de passe</a><br><br>";
        $msg .= "Email automatique - Ne pas répondre";

        $this->email->message($msg);
        $this->email->to($this->receiver_email);
        $result = $this->email->send();
        if (!$result) {
            echo "Erreur lors de l'envoi de l'email: " . $this->email->print_debugger();
        } else {
            echo "Email envoyé avec succès";
        }
    }

    public function confirm()
    {
        $token = '1234567890';
        $nickname = 'hello';
        $config = $this->smtp_config();
        $this->load->library('email', $config);
        $this->email->from($config['from_email'], $config['from_name']);
        $this->email->to($this->receiver_email);
        $url = 'https://www.ygg.re/user/confirm_account?token=' . $token;
        
        // Message avec uniquement des balises br et a
        $msg = "Bienvenue sur Ygg<br><br>";
        $msg .= "Bonjour " . $nickname . ",<br><br>";
        $msg .= "Pour activer votre compte, cliquez sur le lien ci-dessous :<br><br>";
        $msg .= "<a href=\"" . $url . "\">" . $url . "</a><br><br>";
        $msg .= "Email automatique - Ne pas répondre";
        
        $this->email->subject('Activation de votre compte Ygg');
        $this->email->message($msg);
        $result = $this->email->send();
        if (!$result) {
            echo "Erreur lors de l'envoi de l'email: " . $this->email->print_debugger();
        } else {
            echo "Email envoyé avec succès";
        }
    }

}