package Daje::Plugin::Workflow;
use Mojo::Base 'Mojolicious::Plugin', -signatures;
use v5.40;

# NAME
# ====
#
# Daje::Plugin::Workflow - Plugin to use Daje::Wrokflow
#
# SYNOPSIS
# ========
#
#     use Daje::Plugin::Workflow;
#
#     Expected config data
#
#     workflow => {
#         loader => {
#             path => '/home/xxxx/Project/Workflows',
#             type => 'workflow'
#         }
#     }
#
# DESCRIPTION
# ===========
#
# Daje::Plugin::Workflow is the Mojolicious plugin for Daje::Workflow
#
# LICENSE
# =======
#
# Copyright (C) janeskil1525.
#
# This library is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
# AUTHOR
# ======
#
# janeskil1525 E<lt>janeskil1525@gmail.comE<gt>
#

our $VERSION = "0.45";

use Data::Dumper;
use Daje::Workflow::Database;
use Daje::Workflow::Loader;
use Daje::Workflow;
use Daje::Database::Migrator;

sub register ($self, $app, $config) {

    $app->log->debug("Daje::Plugin::Workflow::register starts");

    $self->create_workflow_engine($app);

    my $migration->{class} = 'Daje::Workflow::Database';
    $migration->{name} = 'workflow';
    $migration->{migration} = 4;
    my $migrations;
    push @{$migrations}, $migration;
    try {
        Daje::Database::Migrator->new(
            pg         => $app->pg,
            migrations => $migrations,
        )->migrate();
    } catch ($e) {
        $app->log->error($e);
    };

    if (exists $app->config('workflow')->{minion} && $app->config('workflow')->{minion} == 1) {
        $app->minion->add_task(execute_workflow => \&_execute_workflow);
    }

    $self->setup_controller($app);
    $app->log->debug("Daje::Plugin::Workflow registered");
}

sub _execute_workflow($job, $context) {

    try {
        if(exists $context->{context}->{payload}->{workflow_fkey}) {
            $job->app->workflow_engine->workflow_pkey($context->{context}->{payload}->{workflow_fkey});
        } else {
            $job->app->workflow_engine->workflow_pkey($context->{context}->{workflow}->{workflow_fkey});
        }
        $job->app->workflow_engine->workflow_name($context->{context}->{workflow}->{workflow});
        $job->app->workflow_engine->context($context);
        $job->app->workflow_engine->process($context->{context}->{workflow}->{activity});
        if($job->app->workflow_engine->error->has_error() == 0) {
            my $message = $context->{context}->{workflow}->{workflow} . " and activity " .  $context->{context}->{workflow}->{activity} . " ended successfully";
            $job->finish((message => $message ));
        } else {
            $job->fail((errors => ['Daje::Controller::Workflows::Workflows::execute ' . $job->app->workflow_engine->error->error()]));
        }
    } catch ($e) {
        say $e;
    }
}

sub setup_controller($self, $app) {

    push @{$app->routes->namespaces}, 'Daje::Controller::Workflows';
    my $r = $app->auth;
    $r->put('workflow/execute_que')->to('Workflowsque#execute');
    $r->put('workflow/execute')->to('Workflows#execute');
}

sub create_workflow_engine($self, $app) {
    my $loader;
    try { # '/home/jan/Project/Daje-Workflow-Workflows/Workflows'
        $loader = Daje::Workflow::Loader->new(
            path => $app->config('workflow')->{loader}->{path},
            type => $app->config('workflow')->{loader}->{type},
        );
        $loader->load();
    } catch ($e) {
        $app->log->error($e);
    };

    my $workflow_engine;
    try {
        $workflow_engine = Daje::Workflow->new(
            pg     => $app->pg,
            loader => $loader->loader,
        );
    } catch ($e) {
        $app->log->error($e);
    };
    $app->helper(workflow_engine => sub {$workflow_engine});
}



1;

__END__









#################### pod generated by Pod::Autopod - keep this line to make pod updates possible ####################

=head1 NAME


Daje::Plugin::Workflow - Plugin to use Daje::Wrokflow



=head1 SYNOPSIS


    use Daje::Plugin::Workflow;

    Expected config data

    workflow => {
        loader => {
            path => '/home/xxxx/Project/Workflows',
            type => 'workflow'
        }
    }



=head1 DESCRIPTION


Daje::Plugin::Workflow is the Mojolicious plugin for Daje::Workflow



=head1 REQUIRES

L<Daje::Database::Migrator> 

L<Daje::Workflow> 

L<Daje::Workflow::Loader> 

L<Daje::Workflow::Database> 

L<v5.40> 

L<Mojo::Base> 


=head1 METHODS

=head2 register

 register();


=head1 AUTHOR


janeskil1525 E<lt>janeskil1525@gmail.comE<gt>



=head1 LICENSE


Copyright (C) janeskil1525.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.



=cut

