package Daje::Workflow::Activities::Companies::New;
use Mojo::Base 'Daje::Workflow::Common::Activity::Base', -base, -signatures;
use v5.42;

# NAME
# ====
#
# Daje::Workflow::Activities::Companies::Activity - A workflow activity
#
# SYNOPSIS
# ========
#;
#
# DESCRIPTION
# ===========
#
# Daje::Workflow::Activities::Companies::Activity is a Daje workflow activity.
#
# METHODS
# =======
#
# Daje::Workflow::Activities::Companies::Activity inherits all methods from
# Daje::Workflow::Activities::Tools::Generate::Base and implements the following new ones.
#
# activity
#
# SEE ALSO
# ========
#
# Mojolicious, Mojolicious::Guides, https://mojolicious.org.
#
# 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.com
#

# This file is generated once automatically by Daje Tools 2026-02-02 15:02:14.
# It will not be touched by Daje Tools again.

# <!-- Autogenerated file 2026-02-02 15:02:14 -->

use Daje::Database::Model::CompaniesCompanies;
use Daje::Database::Model::CompaniesUsers;
use Daje::Database::Model::UsersUsers;
use Data::Dumper;

sub save_new_companies_companies($self) {

    $self->model->insert_history(
        "New client",
        "Daje::Workflow::Activities::Companies::New::save_new_companies_companies",
        1
    );

    try {
        my $data = $self->context->{context}->{payload};
        my $user = $data->{user};
        $data->{companies_workflow_fkey} = $self->context->{context}->{workflow}->{workflow_fkey};
        delete %$data{user};
        my $companies_companies_pkey = Daje::Database::Model::CompaniesCompanies->new(
            db => $self->db
        )->insert($data)->{data}->{companies_companies_pkey};

        my $companies_user->{users_users_fkey} = $user->{users_users_pkey};
        $companies_user->{companies_companies_fkey} = $companies_companies_pkey;
        Daje::Database::Model::CompaniesUsers->new(
            db => $self->db
        )->insert($companies_user);

        my $active_user->{active} = "true";
        $active_user->{users_users_pkey} = $user->{users_users_pkey};;
        Daje::Database::Model::UsersUsers->new(
            db => $self->db
        )->update($active_user);

        $self->model->insert_history(
            "New client " . $data->{name},
            "Daje::Workflow::Activities::Companies::New::save_new_companies_companies",
            2
        );
    } catch ($e) {
        $self->error->add_error($e)
    }

}

1;

