package Cpanel::Plugins::Components::whostmgr::home::360_stats_widget;

use v5.26;    # For: Indented heredoc
use feature 'state';
use Moo;
use cPstrict;

use Cpanel::Imports;

extends 'Cpanel::Plugins::Components::360::BaseWidget';
with 'Cpanel::Plugins::Components::Role::Template';

has '+js_url' => (
    default => 'plugin_components/360-stats-widget/dist/cp-360-stats-widget.cmb.min.js',
);

has '+markup' => (
    default => sub {
        require Cpanel::Encoder::Tiny;

        my $title = Cpanel::Encoder::Tiny::safe_html_encode_str( "Server Monitoring" );

        # Translations for stoplight widget
        my $monitors = Cpanel::Encoder::Tiny::safe_html_encode_str(
            Cpanel::JSON::Dump(
                {
                    cpu    => Cpanel::Encoder::Tiny::safe_html_encode_str( locale()->maketext("CPU") ),
                    memory => Cpanel::Encoder::Tiny::safe_html_encode_str( locale()->maketext("Memory") ),
                    disk   => Cpanel::Encoder::Tiny::safe_html_encode_str( locale()->maketext("Disk") ),
                }
            )
        );
        my $alert_levels = Cpanel::Encoder::Tiny::safe_html_encode_str(
            Cpanel::JSON::Dump(
                {
                    cpu => {
                        critical => locale()->maketext("Your server has used almost all its CPU resources. This could impact performance."),
                        warning  => locale()->maketext("Your server has used most of its CPU resources."),
                        healthy  => locale()->maketext("Your server has enough CPU resources."),
                        unknown  => locale()->maketext("No CPU data available for your server."),
                    },
                    memory => {
                        critical => locale()->maketext("Your server has used almost all its memory resources. This could impact performance."),
                        warning  => locale()->maketext("Your server has used most of its memory resources."),
                        healthy  => locale()->maketext("Your server has enough memory resources."),
                        unknown  => locale()->maketext("No memory data available for your server."),
                    },
                    disk => {
                        critical => locale()->maketext("Your server has used almost all its disk space. This could impact performance."),
                        warning  => locale()->maketext("Your server has used most of its disk space."),
                        healthy  => locale()->maketext("Your server has enough disk space."),
                        unknown  => locale()->maketext("No disk data available for your server."),
                    },
                }
            )
        );

        # Translations for the notice widget
        # TODO: I want to remove this from the design.
        # my $link_alerts_title = Cpanel::Encoder::Tiny::safe_html_encode_str( locale()->maketext("Alerts") );
        # my $text_alerts       = Cpanel::Encoder::Tiny::safe_html_encode_str( locale()->lextext("[quant,_1,Alert,Alerts,No alerts]") );
        my $text_loading = Cpanel::Encoder::Tiny::safe_html_encode_str( locale()->maketext("Loading …") );
        my $text_error = Cpanel::Encoder::Tiny::safe_html_encode_str( locale()->maketext("Error loading monitors.") );

        # Signup button
        my $signup_button_text = Cpanel::Encoder::Tiny::safe_html_encode_str( locale()->maketext("Sign up for email alerts") );

        # Fetch the base urls stuff
        my $is_staging   = -e '/var/cpanel/use_monitoring_stage' ? 1                                    : 0;
        my $base_url     = $is_staging                           ? "https://api.stage.monitoring360.io" : "https://api.monitoring360.io";
        my $base_url_enc = Cpanel::Encoder::Tiny::safe_html_encode_str($base_url);

        # Fetch the API information
        my $api_key      = Cpanel::Plugins::Components::360::BaseWidget::get_api_key();
        my $base_key_enc = Cpanel::Encoder::Tiny::safe_html_encode_str($api_key);

        return <<~"END_TEMPLATE";
        <script id="cp-360-stats-widget-template" type="text/html">
            <cp-360-stats-widget
                title="$title"
                monitors="$monitors"
                alert_levels="$alert_levels"
                signup_button_text="$signup_button_text"
                loading_text="$text_loading"
                error_text="$text_error"
                api_key="$base_key_enc"
                api_base_url="$base_url_enc">
            </cp-360-stats-widget>
        </script>
        END_TEMPLATE
    }
);

1;
