-- Smilegate PoC backoffice identity administration model.
-- Tool operators are distinct from game-service users and from the admin login.

set define off

declare
  procedure create_if_missing(p_sql varchar2) is
  begin
    execute immediate p_sql;
  exception
    when others then
      if sqlcode != -955 then raise; end if;
  end;
begin
  create_if_missing('create table sg_app_user (
    user_id number primary key,
    login_id varchar2(100) not null unique,
    user_name varchar2(200) not null,
    employee_no varchar2(100),
    dept_code varchar2(100),
    can_read_contents char(1) default ''N'' not null,
    active char(1) default ''Y'' not null,
    created_at timestamp default systimestamp not null,
    updated_at timestamp default systimestamp not null,
    constraint sg_app_user_active_ck check (active in (''Y'', ''N''))
  )');
  create_if_missing('create table sg_app_group (
    group_id number primary key,
    group_code varchar2(100) not null unique,
    group_name varchar2(200) not null,
    description varchar2(500),
    active_yn char(1) default ''Y'' not null,
    created_at timestamp default systimestamp not null,
    updated_at timestamp default systimestamp not null,
    constraint sg_app_group_active_ck check (active_yn in (''Y'', ''N''))
  )');
  create_if_missing('create table sg_app_role (
    role_id number primary key,
    role_name varchar2(100) not null unique,
    description varchar2(500),
    max_sensitivity_level varchar2(30) default ''INTERNAL'' not null,
    created_at timestamp default systimestamp not null,
    updated_at timestamp default systimestamp not null,
    constraint sg_app_role_sensitivity_ck check (max_sensitivity_level in (''PUBLIC'', ''INTERNAL'', ''CONFIDENTIAL'', ''RESTRICTED''))
  )');
  create_if_missing('create table sg_user_role (
    user_id number not null,
    role_id number not null,
    created_at timestamp default systimestamp not null,
    constraint sg_user_role_pk primary key (user_id, role_id),
    constraint sg_user_role_user_fk foreign key (user_id) references sg_app_user(user_id),
    constraint sg_user_role_role_fk foreign key (role_id) references sg_app_role(role_id)
  )');
  create_if_missing('create table sg_user_group (
    group_id number not null,
    user_id number not null,
    created_at timestamp default systimestamp not null,
    constraint sg_user_group_pk primary key (group_id, user_id),
    constraint sg_user_group_group_fk foreign key (group_id) references sg_app_group(group_id),
    constraint sg_user_group_user_fk foreign key (user_id) references sg_app_user(user_id)
  )');
  create_if_missing('create table sg_group_role (
    group_id number not null,
    role_id number not null,
    created_at timestamp default systimestamp not null,
    constraint sg_group_role_pk primary key (group_id, role_id),
    constraint sg_group_role_group_fk foreign key (group_id) references sg_app_group(group_id),
    constraint sg_group_role_role_fk foreign key (role_id) references sg_app_role(role_id)
  )');
  create_if_missing('create table sg_audit_event (
    audit_id number primary key,
    event_type varchar2(100) not null,
    key_id number,
    object_id number,
    status varchar2(30) not null,
    row_count number,
    error_code varchar2(100),
    message varchar2(2000),
    created_at timestamp default systimestamp not null
  )');
  create_if_missing('create sequence sg_audit_event_seq start with 1 increment by 1 nocache');
end;
/

merge into sg_app_user t
using (
  select 1001 user_id, 'sg-teamlead' login_id, 'SG Demo Team Lead' user_name, 'SG-001' employee_no, 'DATA_AI' dept_code from dual
  union all
  select 1002, 'sg-member', 'SG Demo Team Member', 'SG-002', 'DATA_AI' from dual
) s on (t.user_id = s.user_id)
when matched then update set t.login_id=s.login_id, t.user_name=s.user_name, t.employee_no=s.employee_no, t.dept_code=s.dept_code, t.active='Y', t.updated_at=systimestamp
when not matched then insert (user_id, login_id, user_name, employee_no, dept_code, can_read_contents, active)
values (s.user_id, s.login_id, s.user_name, s.employee_no, s.dept_code, 'N', 'Y');

merge into sg_app_group t
using (select 2001 group_id, 'DATA_AI_TF' group_code, 'Data & AI TF' group_name, 'Smilegate Data and AI proof-of-concept operators' description from dual) s
on (t.group_id = s.group_id)
when matched then update set t.group_code=s.group_code, t.group_name=s.group_name, t.description=s.description, t.active_yn='Y', t.updated_at=systimestamp
when not matched then insert (group_id, group_code, group_name, description, active_yn) values (s.group_id, s.group_code, s.group_name, s.description, 'Y');

merge into sg_app_role t
using (
  select 3001 role_id, 'DATA_AI_TEAM_LEAD' role_name, 'Data and AI TF lead' description, 'CONFIDENTIAL' max_sensitivity_level from dual
  union all
  select 3002, 'DATA_AI_TEAM_MEMBER', 'Data and AI TF member', 'INTERNAL' from dual
) s on (t.role_id=s.role_id)
when matched then update set t.role_name=s.role_name, t.description=s.description, t.max_sensitivity_level=s.max_sensitivity_level, t.updated_at=systimestamp
when not matched then insert (role_id, role_name, description, max_sensitivity_level) values (s.role_id, s.role_name, s.description, s.max_sensitivity_level);

merge into sg_user_group t using (select 2001 group_id, 1001 user_id from dual union all select 2001, 1002 from dual) s
on (t.group_id=s.group_id and t.user_id=s.user_id) when not matched then insert (group_id, user_id) values (s.group_id, s.user_id);

merge into sg_user_role t using (select 1001 user_id, 3001 role_id from dual union all select 1002, 3002 from dual) s
on (t.user_id=s.user_id and t.role_id=s.role_id) when not matched then insert (user_id, role_id) values (s.user_id, s.role_id);

merge into sg_group_role t using (select 2001 group_id, 3002 role_id from dual) s
on (t.group_id=s.group_id and t.role_id=s.role_id) when not matched then insert (group_id, role_id) values (s.group_id, s.role_id);

commit;
