<?php
/**
 * OVP-Sponsorizare
 *
 * @package   OVPSPO_Sponsorizare
 * @author    Ovidiu Pantiru - OVP-Digital
 * @copyright 2024 Ovidiu Pantiru
 * @license   GPL-2.0+
 * @link      https://ovidiupantiru.ro
 */

if ( ! defined( 'ABSPATH' ) ) exit;

if ( ! class_exists('OVPSPO_Email') ) {
class OVPSPO_Email {

    public static function send_admin( $data, $attachments = array() ) {
        $to      = OVPSPO_Settings::get( 'org_email_admin', get_option('admin_email') );
        $subject = self::replace_vars( OVPSPO_Settings::get( 'email_subiect_admin', 'Formular 230 nou - {nume} {prenume}' ), $data );
        $body    = self::replace_vars( self::body_admin(), $data );
        self::send( $to, $subject, $body, $attachments );
    }

    public static function send_user( $data, $attachments = array() ) {
        if ( empty( $data['email'] ) ) return;
        $subject = self::replace_vars( OVPSPO_Settings::get( 'email_subiect_user', 'Formularul tau 230 - {org_nume}' ), $data );
        $body    = self::replace_vars( self::body_user(), $data );
        self::send( $data['email'], $subject, $body, $attachments );
    }

    private static function send( $to, $subject, $body, $attachments = array() ) {
        $from_email = OVPSPO_Settings::get( 'org_email_from' );
        $from_name  = OVPSPO_Settings::get( 'org_email_from_name' );

        if ( $from_email ) {
            add_filter( 'wp_mail_from', function( $email ) use ( $from_email ) {
                return $from_email;
            }, 99 );
        }
        if ( $from_name ) {
            add_filter( 'wp_mail_from_name', function( $name ) use ( $from_name ) {
                return $from_name;
            }, 99 );
        }

        $headers = array( 'Content-Type: text/html; charset=UTF-8' );

        // Filtreaza doar fisierele care exista pe server
        $valid_attachments = array();
        if ( ! empty( $attachments ) ) {
            foreach ( $attachments as $att ) {
                if ( $att && file_exists( $att ) ) {
                    $valid_attachments[] = $att;
                }
            }
        }

        wp_mail( $to, $subject, $body, $headers, $valid_attachments );
    }

    private static function replace_vars( $text, $data ) {
        $adresa = 'Str. ' . $data['strada'] . ' nr. ' . $data['nr'];
        if ( $data['bloc'] )  $adresa .= ', bl. ' . $data['bloc'];
        if ( $data['scara'] ) $adresa .= ', sc. ' . $data['scara'];
        if ( $data['etaj'] )  $adresa .= ', et. ' . $data['etaj'];
        if ( $data['apart'] ) $adresa .= ', ap. ' . $data['apart'];
        $adresa .= ', ' . $data['localitate'] . ', jud. ' . $data['judet'];

        $vars = array(
            '{nume}'        => $data['nume'],
            '{prenume}'     => $data['prenume'],
            '{cnp}'         => $data['cnp'],
            '{email}'       => $data['email'] ? $data['email'] : '—',
            '{telefon}'     => $data['telefon'] ? $data['telefon'] : '—',
            '{judet}'       => $data['judet'],
            '{localitate}'  => $data['localitate'],
            '{adresa}'      => $adresa,
            '{perioada}'    => $data['perioada'] == 2 ? 'Doi ani' : 'Un an',
            '{an_fiscal}'   => $data['an_fiscal'],
            '{data}'        => gmdate('d.m.Y H:i'),
            '{org_nume}'    => OVPSPO_Settings::get( 'org_nume', '' ),
            '{org_website}' => OVPSPO_Settings::get( 'org_website', '' ),
            '{org_telefon}' => OVPSPO_Settings::get( 'org_telefon', '' ),
        );

        return str_replace( array_keys( $vars ), array_values( $vars ), $text );
    }

    private static function body_admin() {
        return '<p>A fost completat un nou <strong>Formular 230</strong>.</p>
<table style="border-collapse:collapse;width:100%;max-width:520px;font-family:sans-serif">
<tr style="background:#f5f5f5"><td style="padding:8px;font-weight:bold">Contribuabil:</td><td style="padding:8px">{nume} {prenume}</td></tr>
<tr><td style="padding:8px;font-weight:bold">CNP:</td><td style="padding:8px">{cnp}</td></tr>
<tr style="background:#f5f5f5"><td style="padding:8px;font-weight:bold">Adresa:</td><td style="padding:8px">{adresa}</td></tr>
<tr><td style="padding:8px;font-weight:bold">Email:</td><td style="padding:8px">{email}</td></tr>
<tr style="background:#f5f5f5"><td style="padding:8px;font-weight:bold">Telefon:</td><td style="padding:8px">{telefon}</td></tr>
<tr><td style="padding:8px;font-weight:bold">Perioada:</td><td style="padding:8px">{perioada}</td></tr>
<tr style="background:#f5f5f5"><td style="padding:8px;font-weight:bold">An fiscal:</td><td style="padding:8px">{an_fiscal}</td></tr>
<tr><td style="padding:8px;font-weight:bold">Data:</td><td style="padding:8px">{data}</td></tr>
</table>
<p>Este atasat <strong>formularul oficial ANAF</strong> (PDF conform OPANAF 103/2025).</p>';
    }

    private static function body_user() {
        return '<p>Buna ziua, <strong>{prenume} {nume}</strong>,</p>
<p>Va multumim ca ati completat Formularul 230 pentru <strong>{org_nume}</strong>.</p>
<p>Găsiți atașat emailului <strong>formularul oficial ANAF</strong> – documentul conform legislației în vigoare (OPANAF 103/2025).</p>
<p><strong>Echipa noastra se va ocupa de depunerea la ANAF. Nu trebuie sa faceti nimic altceva!</strong></p>
<p>Cu stima,<br><strong>{org_nume}</strong><br>{org_website}<br>{org_telefon}</p>';
    }
}
} // end class_exists
