<?php

namespace HelpGent\App\Mail;

defined( 'ABSPATH' ) || exit;

use stdClass;

class NewResponseMailer extends Mailer {
    public stdClass $response;

    public ?array $created_by;

    public array $to_user;

    public array $site_admin;

    public string $response_token;

    public ?string $dashboard_page_url;

    public function __construct( stdClass $response, array $template_settings, ?array $created_by, array $to_user, string $response_token ) {
        $this->response           = $response;
        $this->template_settings  = $template_settings;
        $this->created_by         = $created_by;
        $this->to_user            = $to_user;
        $this->to                 = (string) ( $to_user['email'] ?? '' );
        $this->response_token     = $response_token;
        $this->dashboard_page_url = helpgent_get_dashboard_page_url();
        
        $site_admin    = $this->get_site_admin();
        $this->headers = [ 'From' => "{$site_admin['name']} <{$site_admin['email']}>" ];

        $this->prepare_placeholders();
        $this->prepare_email_template_settings();
    }

    public function get_body(): string {
        return $this->get_email_template( $this->prepare_content( $this->body ) );
    }

    public function prepare_placeholders() {
        $this->placeholders = [
            '{{FROM_NAME}}'      => $this->created_by ? $this->created_by['name'] : __( 'anonymous user', 'helpgent' ),
            '{{FROM_EMAIL}}'     => $this->created_by ? $this->created_by['email'] : '',
            '{{TO_NAME}}'        => $this->to_user['name'] ?? '',
            '{{TO_EMAIL}}'       => $this->to_user['email'] ?? '',
            '{{NOW}}'            => helpgent_now()->wp_date_time_string( $this->response->created_at ),
            '{{IP_ADDRESS}}'     => $this->response->ip,
            '{{DASHBOARD_LINK}}' => $this->get_dashboard_page_link( 'Take Me to Dashboard' ),
        ];

        $this->placeholders['{{VERIFICATION_LINK}}'] = $this->get_dashboard_page_link( 'Verify and Take Me to Dashboard', 'verify' );
        $this->placeholders['{{CANCEL_LINK}}']       = $this->get_dashboard_page_link( "No, It Wasn't Me", 'cancel' );
    }

    public function get_dashboard_page_link( string $label, ?string $link_type = null ) {
        $dashboard_page_url = $this->dashboard_page_url;
        $class              = 'link-primary';
        $args               = [ 'form_id' => $this->response->form_id ];

        switch ( $link_type ) {
            case 'verify':
                $args[ 'hg_response_token' ] = $this->response_token;
                $args[ 'verify' ]            = 1;
                break;
            case 'cancel':
                $class                       = 'link-danger';
                $args[ 'hg_response_token' ] = $this->response_token;
                $args[ 'cancel' ]            = 1;
                break;
        }

        if ( ! empty( $this->to_user['token'] ) ) {
            $args['hg_token'] = $this->to_user['token'];
        }

        $dashboard_page_url = add_query_arg( $args, $this->dashboard_page_url );

        return "<a class='{$class}' href='{$dashboard_page_url}'>{$label}</a>";
    }
}
