<?php

namespace MovieEngine\Emails;

if (! defined("ABSPATH")) {
    exit; // Exit if accessed directly
}

class EmailManager {
    public function __construct() {
        add_action('movie_engine_payment_completed', [$this, 'send_payment_receipt'], 10, 2);
        add_action('movie_engine_membership_activated', [$this, 'send_welcome_email'], 10, 2);
        add_action('user_register', [$this, 'on_user_register']);
        add_action('wp_login', [$this, 'send_login_alert'], 10, 2);
    }

    public function send_payment_receipt($transaction_id, $data) {
        $transaction = apply_filters('movie_engine_get_transaction', null, $transaction_id);
        if (!$transaction) return;

        $user = get_userdata($transaction->user_id);
        if (!$user) return;

        $email = new Types\ReceiptEmail();
        $email->trigger($user, $transaction);
    }

    public function send_welcome_email($user_id, $plan_id) {
        $user = get_userdata($user_id);
        if (!$user) return;

        $email = new Types\WelcomeEmail();
        $email->trigger($user, $plan_id);
    }

    public function on_user_register($user_id) {
        $user = get_userdata($user_id);
        if (!$user) return;

        $email = new Types\WelcomeEmail();
        $email->trigger($user);
    }

    public function send_login_alert($user_login, $user) {
        if (!is_object($user)) return;

        $ua_string = isset($_SERVER['HTTP_USER_AGENT']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT'])) : '';
        $ip = isset($_SERVER['REMOTE_ADDR']) ? sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])) : '';
        $device_hash = md5($ua_string . '|' . $ip);

        $known_devices = get_user_meta($user->ID, '_movie_engine_known_devices', true);
        if (!is_array($known_devices)) {
            $known_devices = [];
        }

        if (in_array($device_hash, $known_devices)) {
            return;
        }

        $known_devices[] = $device_hash;
        if (count($known_devices) > 10) {
            array_shift($known_devices);
        }
        update_user_meta($user->ID, '_movie_engine_known_devices', $known_devices);

        $browser = 'Unknown Browser';
        if (strpos($ua_string, 'Chrome') !== false) $browser = 'Chrome';
        elseif (strpos($ua_string, 'Firefox') !== false) $browser = 'Firefox';
        elseif (strpos($ua_string, 'Safari') !== false) $browser = 'Safari';
        elseif (strpos($ua_string, 'Edge') !== false) $browser = 'Edge';

        $os = 'Unknown OS';
        if (strpos($ua_string, 'Mac') !== false) $os = 'macOS';
        elseif (strpos($ua_string, 'Windows') !== false) $os = 'Windows';
        elseif (strpos($ua_string, 'Android') !== false) $os = 'Android';
        elseif (strpos($ua_string, 'iPhone') !== false) $os = 'iOS';

        $login_data = [
            'browser' => $browser,
            'os'      => $os,
            'ip'      => $ip
        ];

        $email = new Types\SecurityEmail();
        $email->trigger($user, $login_data);
    }
}
