create database if not exists FPDAM2023 character set utf8mb4 collate utf8mb4_general_ci;
use FPDAM2023;

delimiter ;^

	drop procedure if exists base_set;^
	create procedure base_set(
		in `$session` integer
	) begin 
		
		declare `$database` varchar(64) default AnP.settings_get('database_fpdam2023', false);
		declare `$procedure` varchar(64) default 'base_set';
		declare $procedure_id integer;
		declare $id integer;
		declare $has_database boolean default `$database` is not null;
		declare $session_id integer default AnP.sessions_get_id(`$session`);
		declare $ip_id integer;
		declare $user_agent_id integer;
	
		if $session_id is null then 
			set $session_id := AnP.sessions_get_machine();
		end if;

		select ip_id, user_agent_id into $ip_id, $user_agent_id from AnP.SessionsFullView where id = $session_id limit 1;
	
		if `$database` is null then 
			set `$database` := 'FPDAM2023';
		end if;
			
		call AnP.procedures_get(`$database`, `$procedure`, $procedure_id);
	
		if !$has_database then begin 
			insert into AnP.Settings values();
			set $id := last_insert_id();
			insert into AnP.SettingsData(`procedure`, `session`, entity, `type`, name, `range`, value) values 
				($procedure_id, $session_id, $id, 1, 'database_fpdam2023', 1, 'FPDAM2023');
		end;end if;
	
		call AnP.logs_set($session_id, `$database`, `$procedure`, $ip_id, $user_agent_id, 0, '{}', 1 << 1);
		
	end;^
	
	call base_set(null);^

delimiter ;