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

# Autogenerated class Thu Jun 26 17:06:45 2025
# Any manual changes to this class will be overwritten next time its generated

use Data::Dumper;

has 'db';

sub load_from_index($self, $table, $select, $condition ) {
    my $result->{result} = 0;
    my $load = $self->db->select(
        $table,
            $select,
            $condition
    );
    $result->{data} = {};
    $result->{data} = $load->hash if $load and $load->rows > 0;
    $result->{result} = 1;
    return $result;

}

sub load_pk($self, $table, $select, $primary_key_name, $primary_key) {
    my $result->{result} = 0;
    my $load = $self->db->select(
        $table,
            $select,
            {$primary_key_name => $primary_key}
    );
    $result->{data} = {};
    $result->{data} = $load->hash if $load and $load->rows > 0;
    $result->{result} = 1;

    return $result;
};

sub load_fkey($self, $table, $select, $foreign_key_name, $foreign_key) {
    my $result->{result} = 0;
    my $load = $self->db->select(
        $table,
            $select,
        {$foreign_key_name => $foreign_key}
    );
    $result->{data} = [];
    $result->{data} = $load->hashes if $load and $load->rows > 0;
    $result->{result} = 1;
    return $result;
}

sub load_a_list($self, $table, $select, $key_value) {
    my $result->{result} = 0;
    my $load = $self->db->select(
        $table,
        $select,
        $key_value
    );
    $result->{data} = [];
    $result->{data} = $load->hashes if $load and $load->rows > 0;
    $result->{result} = 1;
    return $result;
}

sub load_a_full_list($self, $table, $select) {
    my $result->{result} = 0;
    my $load = $self->db->select(
        $table,
        $select
    );
    $result->{data} = [];
    $result->{data} = $load->hashes if $load and $load->rows > 0;
    $result->{result} = 1;
    return $result;
}

sub insert($self, $table, $data, $primary_key_name) {
    my $result->{result} = 0;
    $data = $self->insert_defaults($data);
    my $primary_key = $self->db->insert(
        $table, $data,
            { returning => $primary_key_name }
    );
    $result->{data} = {};
    $result->{data} = $primary_key->hash
        if $primary_key and $primary_key->rows > 0;
    $result->{result} = 1;
    return $result;
}

sub update($self, $table, $data, $keys) {
    my $result->{result} = 0;
    $data = $self->update_defaults($data);
    $self->db->update($table, $data, $keys);
    $result->{result} = 1;
    return $result;
}

1;

