#                                      Copyright 2025 WebPros International, LLC
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited.

package Cpanel::Xovi::LifeCycle;

=encoding utf8

=head1 NAME

Cpanel::Xovi::LifeCycle

=head1 DESCRIPTION

Setup and teardown functionality for cpanel-xovi-plugin.

=cut

use cPstrict;
use Common::JSONC                      ();
use Common::Plugins::Setup::Steps      ();
use Cpanel::Exception                  ();
use Cpanel::Plugins::Components::Rules ();
use Cpanel::Imports;

my $APP_NAME      = 'SEO (by XOVI)';
my $PLUGIN_DIR    = '/var/cpanel/plugins/xovi';
my $DEFAULT_THEME = 'jupiter';

my %INSTALL_STEPS = (
    "/usr/local/cpanel/scripts/install_plugin" => {
        args => [
            "--theme=$DEFAULT_THEME",
            $PLUGIN_DIR,
        ],
        message => "Installing the $APP_NAME plugin"
    },
);

my %UNINSTALL_STEPS = (
    "/usr/local/cpanel/scripts/uninstall_plugin" => {
        args => [
            "--theme=$DEFAULT_THEME",
            $PLUGIN_DIR,
        ],
        message => "Uninstalling the $APP_NAME plugin"
    },
);

=head2 is_setup_already()

Check if the plugin is already setup. May not be needed on all plugins.

=head3 RETURNS

1 if the plugin is already setup, 0 otherwise.

=cut

sub is_setup_already() {
    return 0;
}

=head2 install()

Run the installation steps for the plugin.

=head3 RETURNS

1 if successful. Otherwise, throws an exception.

=head3 THROWS

=over

=item Various failure messages.

=back

=cut

sub install () {
    return if is_setup_already();

    my ( $success, $failures ) = Common::Plugins::Setup::Steps::run_steps( $APP_NAME, %INSTALL_STEPS );

    if ($success) {
        return 1;
    }

    if ( $failures && @$failures < keys %INSTALL_STEPS ) {
        die Cpanel::Exception->create( "Some of the steps used to install $APP_NAME failed: " . join( "\n", @$failures ) );
    }
    die Cpanel::Exception->create( "Failed to install $APP_NAME: " . join( "\n", @$failures ) );
}

=head2 uninstall()

Run the removal steps for the plugin.

=head3 RETURNS

1 if successful. Otherwise, throws an exception.

=head3 THROWS

=over

=item Various failure messages.

=back

=cut

sub uninstall () {
    my ( $success, $failures ) = Common::Plugins::Setup::Steps::run_steps( $APP_NAME, %UNINSTALL_STEPS );

    if ($success) {
        return 1;
    }

    if ( $failures && @$failures < keys %UNINSTALL_STEPS ) {
        die Cpanel::Exception->create( "Some of the steps used to uninstall $APP_NAME failed: " . join( "\n", @$failures ) );
    }
    die Cpanel::Exception->create( "Failed to uninstall $APP_NAME: " . join( "\n", @$failures ) );
}

1;
