package Daje::Database::Model::Login;
use Mojo::Base 'Daje::Database::Model::Super::Common::Base', -base, -async_await;
use v5.42;

# Autogenerated class Sun Feb  9 13:11:20 2025
# This class is generated once. Any over rides or special methods should be put here

# NAME
# ====
#
#      Daje::Database::Model::Login - It's the login class for users
#
#
# REQUIRES
# ========
#
# use Mojo::Base;
# use Daje::Database::Model::Super::Users;
#
# DESCRIPTION
# ===========
#
# Daje::Database::Model::Login is logging in users
#
# 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;
our $VERSION = "0.01";

has 'db';

sub login ($self, $mail, $password) {
    my $outcome;
    my $login_stmt = qq{
        SELECT users_users_pkey, mail, users_workflow_fkey
            FROM users_users
           WHERE mail = ? AND password = ? AND active = true
    };

    my $result = $self->db->query($login_stmt,($mail, $password));

    my $hash;
    $hash = $result->hash if $result->rows;
    if(!defined $hash) {
        $login_stmt = qq{
            SELECT count(*) as exists
                FROM users_users
               WHERE mail = ?
            };
        $result = $self->db->query($login_stmt,($mail));
        $hash = $result->hash if $result->rows;
        if($hash->{exists} > 0) {
            $outcome->{status} = "exists";
        } else {
            $outcome->{status} = "nonexistent";
        }

        if($outcome->{status} eq "exists") {
            $login_stmt = qq{
                SELECT count(*) as inactive
                    FROM users_users
                   WHERE mail = ? AND password = ? AND active = false
            };

            $result = $self->db->query($login_stmt, ($mail, $password));
            $hash = $result->hash if $result->rows;
            if ($hash->{inactive} > 0) {
                $hash = {};
                $outcome = $self->is_user_registered($mail, $password);
            } else {
                $outcome->{status} = "wrong_password";
            }
        }
    } else {
        $outcome = $hash;
        $outcome->{status} = "success";
    }
    return $outcome;
}

sub is_user_registered ($self, $mail, $password) {

    my $hash->{status} = "inactive";
    my $stmt = qq{
            SELECT count(*) as has_company
                FROM companies_users
               WHERE users_users_fkey = (SELECT users_users_pkey FROM users_users WHERE mail = ? AND password = ?)
            };
    my $result = $self->db->query($stmt,($mail, $password));
    $hash->{company} = $result->hash->{has_company};
    $hash->{status} = "unregistered" if $hash->{company} == 0;
    if($hash->{status} eq "unregistered") {
        $stmt = qq{
            SELECT users_users_pkey, mail, users_workflow_fkey
                FROM users_users
               WHERE mail = ? AND password = ?
        };
        $result = $self->db->query($stmt,($mail, $password))->hash;
        $hash->{users_users_pkey} = $result->{users_users_pkey};
        $hash->{users_workflow_fkey} = $result->{users_workflow_fkey};
        $hash->{mail} = $result->{mail};
    }

    return $hash;
}

sub check_creds ($self, $mail, $password) {

    my $check_stmt = qq{
        SELECT count(*) as exists
            FROM users_users
           WHERE mail = ? AND password = ? AND (active = true OR verified = false)
    };

    my $result = $self->db->query(
        $check_stmt,($mail, $password)
    )->hash->{exists};

    return $result;
}

1;


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

=head1 NAME

Daje::Database::Model::Login


=head1 DESCRIPTION

Autogenerated class Sun Feb  9 13:11:20 2025
This class is generated once. Any over rides or special methods should be put here


=head1 REQUIRES

L<Digest::SHA> 

L<Data::Dumper> 

L<Mojo::Base> 


=head1 METHODS

=head2 check_creds

 check_creds();

=head2 login

 login();


=cut

