use AnP

if object_id(N'dbo.settings_tables_remove', N'P') is not null drop procedure dbo.settings_tables_remove
go
create procedure dbo.settings_tables_remove as begin set nocount on 

    if object_id(N'dbo._SettingsGroups', N'U') is not null drop table dbo._SettingsGroups
    if object_id(N'dbo._Settings', N'U') is not null drop table dbo._Settings
    if object_id(N'dbo._SettingsTypes', N'U') is not null drop table dbo._SettingsTypes

    if object_id(N'dbo.SettingsGroups', N'U') is not null drop table dbo.SettingsGroups
    if object_id(N'dbo.Settings', N'U') is not null drop table dbo.Settings
    if object_id(N'dbo.SettingsTypes', N'U') is not null drop table dbo.SettingsTypes

end
go

if object_id(N'dbo.settings_tables_create', N'P') is not null drop procedure dbo.settings_tables_create
go
create procedure dbo.settings_tables_create as begin set nocount on 

    if object_id(N'dbo.SettingsTypes', N'U') is null create table dbo.SettingsTypes(
        id integer not null identity(1, 1), 
        [procedure] integer not null, 
        [session] integer not null, 
        [name] varchar(32) not null, 
        [description] varchar(512) not null, 
        date_in datetime not null constraint df_settings_types_date_in default getdate(), 
        date_out datetime, 
        history_in datetime2 generated always as row start hidden not null constraint df_settings_types_history_in default sysutcdatetime(), 
        history_out datetime2 generated always as row end hidden not null constraint df_settings_types_history_out default convert(datetime2, '9999-12-31 23:59:59.9999999'),
        constraint pk_settings_types primary key clustered(id), 
        constraint fk_settings_types_procedure foreign key([procedure]) references dbo.Procedures(id) 
            on update no action 
            on delete no action,
        constraint fk_settings_types_session foreign key([session]) references dbo.Sessions(id) 
            on update no action 
            on delete no action,
        constraint uq_settings_types_name unique([name]) with(fillfactor = 90), 
        constraint ck_settings_types_name check([name] != '' and [name] not like '%[^a-zA-Z0-9_]%' and [name] not like '[^a-zA-Z_]%')
    )

    if object_id(N'dbo.Settings', N'U') is null create table dbo.Settings(
        id integer not null identity(1, 1), 
        [procedure] integer not null, 
        [session] integer not null, 
        [type] integer not null, 
        [name] varchar(64) not null, 
        [value] varchar(max), 
        [default] varchar(max), 
        [description] varchar(512) not null, 
        date_in datetime not null constraint df_settings_date_in default getdate(), 
        date_out datetime, 
        history_in datetime2 generated always as row start hidden not null constraint df_settings_history_in default sysutcdatetime(), 
        history_out datetime2 generated always as row end hidden not null constraint df_settings_history_out default convert(datetime2, '9999-12-31 23:59:59.9999999'),
        constraint pk_settings primary key clustered(id), 
        constraint fk_settings_procedure foreign key([procedure]) references dbo.Procedures(id) 
            on update no action 
            on delete no action,
        constraint fk_settings_session foreign key([session]) references dbo.Sessions(id) 
            on update no action 
            on delete no action,
        constraint fk_settings_type foreign key([type]) references dbo.SettingsTypes(id) 
            on update no action
            on delete no action,
        constraint uq_settings_name unique([name]) with(fillfactor = 90), 
        constraint ck_settings_name check([name] != '' and [name] not like '%[^a-zA-Z0-9_]%' and [name] not like '[^a-zA-Z_]%')
    )

    if object_id(N'dbo.SettingsGroups', N'U') is null create table dbo.SettingsGroups(
        id integer not null identity(1, 1), 
        [procedure] integer not null, 
        [session] integer not null, 
        [settings] integer not null, 
        [group] integer not null,
        belongs bit not null constraint df_settings_groups_belongs default 1,
        date_in datetime not null constraint df_settings_groups_date_in default getdate(), 
        date_out datetime, 
        history_in datetime2 generated always as row start hidden not null constraint df_settings_groups_history_in default sysutcdatetime(), 
        history_out datetime2 generated always as row end hidden not null constraint df_settings_groups_history_out default convert(datetime2, '9999-12-31 23:59:59.9999999'),
        constraint pk_settings_groups primary key clustered(id), 
        constraint fk_settings_groups_procedure foreign key([procedure]) references dbo.Procedures(id) 
            on update no action 
            on delete no action,
        constraint fk_settings_groups_session foreign key([session]) references dbo.Sessions(id) 
            on update no action 
            on delete no action, 
        constraint fk_settings_groups_settings foreign key([settings]) references dbo.Settings(id) 
            on update no action 
            on delete no action,
        constraint fk_settings_groups_group foreign key([group]) references dbo.Users(id) 
            on update no action
            on delete no action, 
        constraint uq_settings_groups unique([settings], [group]) with(fillfactor = 90)
    )

    if object_id(N'dbo._SettingsTypes', N'U') is null create table dbo._SettingsTypes(
        id integer not null identity(1, 1), 
        [procedure] integer not null, 
        [session] integer not null, 
        [name] varchar(32) not null, 
        [description] varchar(512) not null, 
        date_in datetime not null constraint df__settings_types_date_in default getdate(), 
        date_out datetime, 
        history_date datetime2 not null constraint df__settings_types_history_date default sysutcdatetime(), 
        constraint pk__settings_types primary key clustered(id), 
        constraint fk__settings_types_procedure foreign key([procedure]) references dbo.Procedures(id) 
            on update no action 
            on delete no action,
        constraint fk__settings_types_session foreign key([session]) references dbo.Sessions(id) 
            on update no action 
            on delete no action
    )

    if object_id(N'dbo._Settings', N'U') is null create table dbo._Settings(
        id integer not null identity(1, 1), 
        [procedure] integer not null, 
        [session] integer not null, 
        [type] integer not null, 
        [name] varchar(64) not null, 
        [value] varchar(max), 
        [default] varchar(max), 
        [description] varchar(512) not null, 
        date_in datetime not null constraint df_settings_date_in default getdate(), 
        date_out datetime, 
        history_date datetime2 generated always as row start hidden not null constraint df__settings_history_date default sysutcdatetime(), 
        constraint pk__settings primary key clustered(id), 
        constraint fk__settings_procedure foreign key([procedure]) references dbo.Procedures(id) 
            on update no action 
            on delete no action,
        constraint fk__settings_session foreign key([session]) references dbo.Sessions(id) 
            on update no action 
            on delete no action,
        constraint fk__settings_type foreign key([type]) references dbo.SettingsTypes(id) 
            on update no action
            on delete no action,
        constraint uq__settings_name unique([name]) with(fillfactor = 90), 
        constraint ck__settings_name check([name] != '' and [name] not like '%[^a-zA-Z0-9_]%' and [name] not like '[^a-zA-Z_]%')
    )

    if object_id(N'dbo.SettingsGroups', N'U') is null create table dbo._SettingsGroups(
        id integer not null identity(1, 1), 
        [procedure] integer not null, 
        [session] integer not null, 
        [settings] integer not null, 
        [group] integer not null,
        belongs bit not null,
        date_in datetime not null, 
        date_out datetime, 
        history_date datetime2 not null constraint df__settings_groups_history_date default sysutcdatetime(), 
        constraint [REDACTED]primary key clustered(id), 
        constraint fk__settings_groups_procedure foreign key([procedure]) references dbo.Procedures(id) 
            on update no action 
            on delete no action,
        constraint fk__settings_groups_session foreign key([session]) references dbo.Sessions(id) 
            on update no action 
            on delete no action, 
        constraint fk__settings_groups_settings foreign key([settings]) references dbo.Settings(id) 
            on update no action 
            on delete no action,
        constraint fk__settings_groups_group foreign key([group]) references dbo.Users(id) 
            on update no action
            on delete no action, 
        constraint uq__settings_groups unique([settings], [group]) with(fillfactor = 90)
    )

end
go

execute dbo.settings_tables_create

if object_id(N'dbo.settings_types_trigger', N'TR') is not null drop trigger dbo.settings_types_trigger
go
create trigger dbo.settings_types_trigger on dbo.SettingsTypes for insert, update, delete as begin set nocount on 

    declare @insert integer, @update integer, @delete integer, @unknown integer

    execute dbo.history_mode_get @insert output, @update output, @delete output, @unknown output

    -- update.
    insert into dbo._SettingsTypes(mode, entity, [procedure], [session], [name], [description], date_in, date_out) 
    select @update, inserted.id, inserted.[procedure], inserted.[session], inserted.[name], inserted.[description], inserted.date_in, inserted.date_out 
    from inserted inserted 
    join deleted deleted on inserted.id = deleted.id 
    where 
        inserted.[procedure] != deleted.[procedure] or 
        inserted.[session] != deleted.[session] or 
        inserted.[name] != deleted.[name] or 
        inserted.[description] != deleted.[description] or 
        inserted.date_in != deleted.date_in or 
        isnull(inserted.date_out, '0001-01-01') != isnull(deleted.date_out, '0001-01-01')

    -- Insert.
    insert into dbo._SettingsTypes(mode, entity, [procedure], [session], [name], [description], date_in, date_out) 
    select @insert, inserted.id, inserted.[procedure], inserted.[session], inserted.[name], inserted.[description], inserted.date_in, inserted.date_out 
    from inserted inserted 
    left join deleted deleted on inserted.id = deleted.id 
    where deleted.id is null

    -- deleted.
    insert into dbo._SettingsTypes(mode, entity, [procedure], [session], [name], [description], date_in, date_out) 
    select @delete, deleted.id, deleted.[procedure], deleted.[session], deleted.[name], deleted.[description], deleted.date_in, deleted.date_out 
    from deleted deleted 
    left join inserted inserted on deleted.id = inserted.id 
    where inserted.id is null

end
go

if object_id(N'dbo.settings_trigger', N'TR') is not null drop trigger dbo.settings_trigger
go
create trigger dbo.settings_trigger on dbo.Settings for insert, update, delete as begin set nocount on 

    declare @insert integer, @update integer, @delete integer, @unknown integer

    execute dbo.history_mode_get @insert output, @update output, @delete output, @unknown output

    -- update.
    insert into dbo._Settings(mode, entity, [procedure], [session], [type], [name], [value], [default], [description], date_in, date_out) 
    select @update, inserted.id, inserted.[procedure], inserted.[session], inserted.[type], inserted.[name], inserted.[value], inserted.[default], inserted.[description], inserted.date_in, inserted.date_out 
    from inserted inserted 
    join deleted deleted on inserted.id = deleted.id 
    where 
        inserted.[procedure] != deleted.[procedure] or 
        inserted.[session] != deleted.[session] or 
        inserted.[type] != deleted.[type] or 
        inserted.[name] != deleted.[name] or 
        inserted.[value] != deleted.[value] or 
        inserted.[default] != deleted.[default] or 
        inserted.[description] != deleted.[description] or 
        inserted.date_in != deleted.date_in or 
        isnull(inserted.date_out, '0001-01-01') != isnull(deleted.date_out, '0001-01-01')

    -- Insert.
    insert into dbo._Settings(mode, entity, [procedure], [session], [type], [name], [value], [default], [description], date_in, date_out) 
    select @insert, inserted.id, inserted.[procedure], inserted.[session], inserted.[type], inserted.[name], inserted.[value], inserted.[default], inserted.[description], inserted.date_in, inserted.date_out 
    from inserted inserted 
    left join deleted deleted on inserted.id = deleted.id 
    where deleted.id is null

    -- deleted.
    insert into dbo._Settings(mode, entity, [procedure], [session], [type], [name], [value], [default], [description], date_in, date_out) 
    select @delete, deleted.id, deleted.[procedure], deleted.[session], deleted.[type], deleted.[name], deleted.[value], deleted.[default], deleted.[description], deleted.date_in, deleted.date_out 
    from deleted deleted 
    left join inserted inserted on deleted.id = inserted.id 
    where inserted.id is null

end
go

if object_id(N'dbo.settings_groups_trigger', N'TR') is not null drop trigger dbo.settings_groups_trigger
go
create trigger dbo.settings_groups_trigger on dbo.SettingsGroups for insert, update, delete as begin set nocount on 

    declare @insert integer, @update integer, @delete integer, @unknown integer

    execute dbo.history_mode_get @insert output, @update output, @delete output, @unknown output

    -- update.
    insert into dbo._SettingsGroups(mode, entity, [procedure], [session], [settings], [group], [value], date_in, date_out) 
    select @update, inserted.id, inserted.[procedure], inserted.[session], inserted.[settings], inserted.[group], inserted.[value], inserted.date_in, inserted.date_out 
    from inserted inserted 
    join deleted deleted on inserted.id = deleted.id 
    where 
        inserted.[procedure] != deleted.[procedure] or 
        inserted.[session] != deleted.[session] or 
        inserted.[settings] != deleted.[settings] or 
        inserted.[group] != deleted.[group] or 
        inserted.[value] != deleted.[value] or 
        inserted.date_in != deleted.date_in or 
        isnull(inserted.date_out, '0001-01-01') != isnull(deleted.date_out, '0001-01-01')

    -- Insert.
    insert into dbo._SettingsGroups(mode, entity, [procedure], [session], [session], [settings], [group], [value], date_in, date_out) 
    select @insert, inserted.id, inserted.[procedure], inserted.[session], inserted.[settings], inserted.[group], inserted.[value], inserted.date_in, inserted.date_out 
    from inserted inserted 
    left join deleted deleted on inserted.id = deleted.id 
    where deleted.id is null

    -- deleted.
    insert into dbo._SettingsGroups(mode, entity, [procedure], [session], [session], [settings], [group], [value], date_in, date_out) 
    select @delete, deleted.id, deleted.[procedure], deleted.[session], deleted.[settings], deleted.[group], deleted.[value], deleted.date_in, deleted.date_out 
    from deleted deleted 
    left join inserted inserted on deleted.id = inserted.id 
    where inserted.id is null

end
go

if object_id(N'dbo.settings_get', N'FN') is not null drop function dbo.settings_get
go
create function dbo.settings_get(
    @name varchar(64), 
    @group varchar(64) = null, 
    @default varchar(max) = null
) returns varchar(max) as begin 
    if @group is not null begin 
        
        declare @group_id integer = cast(@group as integer)
        declare @value varchar(max) = (
            select top 1 isnull(isnull(isnull(settings_groups.[value], settings.[value]), settings.[default]), @default)
            from dbo.SettingsGroups settings_groups 
            join dbo.Groups groups on groups.id = settings_groups.[group]
            join dbo.Settings settings on settings.id = settings_groups.[settings] 
            where 
                settings_groups.date_out is null and 
                groups.date_out is null and 
                settings.date_out is null and 
                settings.[name] = @name and 
                (groups.id = @group_id or groups.[name] = @group)
        )

        if @value is not null 
            return @value
    end
    return isnull((select top 1 isnull([value], [default]) from dbo.Settings where 
        date_out is null and 
        [name] = @name
    ), @default)
end
go

if object_id(N'dbo.settings_get_int', N'FN') is not null drop function dbo.settings_get_int
go
create function dbo.settings_get_int(
    @name varchar(64), 
    @group varchar(64) = null, 
    @default integer = null
) returns integer as begin 
    
    declare @value varchar(max) = dbo.settings_get(@name, @group, null)

    if @value is null 
        return @default
    return cast(@value as integer)
end
go

if object_id(N'dbo.settings_get_bool', N'FN') is not null drop function dbo.settings_get_bool
go
create function dbo.settings_get_bool(
    @name varchar(64), 
    @group varchar(64) = null, 
    @default bit = null
) returns bit as begin 
    
    declare @value varchar(max) = dbo.settings_get(@name, @group, null)

    if @value is null 
        return @default
    return cast(@value as bit)
end
go