package Daje::Helper::Users::Login;
use Mojo::Base -base, -async_await;
use v5.42;

# NAME
# ====
#
# Daje::Helper::Users::Login - Its a login helper
#
#
# DESCRIPTION
# ===========
#
# my $json = encode_json($login);
#
#
# REQUIRES
# ========
#
# Daje::Helper::Users::Login
#
# Daje::Tools::JWT
#
# v5.42
#
# Mojo::Base
#
#
# METHODS
# =======
#
#
# 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>
#

use Data::Dumper;
use Daje::Tools::JWT;
use Daje::Database::Model::Login;
use Daje::Database::Model::UsersVerificationCodes;
use Daje::Database::Model::CompaniesUsers;
use POSIX;

use Data::Dumper;

use Digest::SHA qw{sha512_base64};

our $VERSION = "0.01";

has 'db';
has 'config';

async sub login_user ($self, $mail, $password) {
    my $result;

    return $result = {
        mail  => $mail,
        status  => 'no_acceptable_password'
    } unless length($password) > 7;

    $password = sha512_base64 $password;

    my $login = Daje::Database::Model::Login->new(
        db => $self->db
    )->login(
        $mail, $password
    );

    my $jwt;
    $login->{password} = $password;
    if(defined $login and exists $login->{users_users_pkey}) {
        $jwt = await Daje::Tools::JWT->new(
            secret => @{$self->config->{secrets}}[0]
        )->encode_jwt_p(
            $login
        );
        my $logged_in = strftime "%Y-%m-%d %H:%M:%S", localtime time;
        $result = {
            mail                => $mail,
            jwt                 => $jwt,
            logged_in           => $logged_in,
            status              => $login->{status},
            users_workflow_fkey => $login->{users_workflow_fkey},
            users_users_pkey    => $login->{users_users_pkey},
        };
    } elsif (defined $login and exists $login->{status}) {
        $result = {
            mail  => $mail,
            status  => $login->{status}
        };
    }
    return $result;
}

async sub post_login($self, $login ) {
    my $claim  = await Daje::Tools::JWT->new(
        secret => @{$self->config->{secrets}}[0]
    )->decode_jwt_p(
        $login->{data}->{jwt}
    );
    $claim->{companies_companies_pkey} = $login->{data}->{companies_companies_pkey};
    $claim->{users_users_pkey} = $login->{data}->{users_users_pkey};
    my $jwt = await Daje::Tools::JWT->new(
         secret => @{$self->config->{secrets}}[0]
     )->encode_jwt_p(
        $claim
     );

    my $logged_in = strftime "%Y-%m-%d %H:%M:%S", localtime time;
    my $result = {
        mail                     => $login->{data}->{mail},
        jwt                      => $jwt,
        logged_in                => $logged_in,
        status                   => $login->{data}->{status},
        users_workflow_fkey      => $login->{users_workflow_fkey},
        users_users_pkey         => $login->{data}->{users_users_pkey},
        companies_companies_pkey => $login->{data}->{companies_companies_pkey},
    };

    return $result;
}

async sub check_verify($self, $mail, $verification_code) {
    return 1 if Daje::Database::Model::UsersVerificationCodes->new(
        db => $self->db
    )->check_verify(
        $mail, $verification_code
    );
    return 0;
}

sub verify($self, $mail) {

    my @set = ('0' ..'9', 'A' .. 'F');
    my $verification_code = join '' => map $set[rand @set], 1 .. 4;

    try {
        Daje::Database::Model::UsersVerificationCodes->new(
            db => $self->db
        )->save_verification_code(
            $mail, $verification_code
        );
    } catch($e) {
        say $e;
        $self->app->log>error($e);
    };

    return $verification_code;
}

sub authenticate ($self, $payload) {
    my $login;
    try {
        my $claim = Daje::Tools::JWT->new(
            secret => @{$self->config->{secrets}}[0]
        )->decode_jwt(
            $payload
        );
        $login = Daje::Database::Model::Login->new(
            db => $self->db
        )->check_creds(
            $claim->{mail}, $claim->{password}
        );
    } catch($e) {
        say $e;
    };

    return $login;
}

1;

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

=head1 NAME


Daje::Helper::Login - Its a




=head1 DESCRIPTION


my $json = encode_json($login);




=head1 REQUIRES


Daje::Database::Model::Login

Daje::Tools::JWT

v5.40

Mojo::Base




=head1 METHODS





=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

