create table if not exists layer3domains(
  id serial not null primary key,
  name varchar(128) not null unique,
  attributes jsonb not null default '{}',
  created_at timestamptz not null default now(),
  created_by varchar(128) not null,
  modified_at timestamptz not null default now(),
  modified_by varchar(128) not null
);

create table if not exists pools(
  id serial not null,
  layer3domain_id integer not null references layer3domains(id),
  name varchar(128) unique,
  attributes jsonb not null default '{}'::jsonb,
  primary key(layer3domain_id, id)
);

create table if not exists container(
  layer3domain_id integer not null references layer3domain(id),
  network cidr not null,
  pool_id integer not null,
  created_at timestamptz not null default now(),
  created_by varchar(128) not null,
  modified_at timestamptz not null default now(),
  modified_by varchar(128) not null,
  attributes jsonb not null default '{}'::jsonb,
  unique(layer3domain_id, network),
  foreign key (layer3domain_id, pool_id) references pools(layer3domain_id, pool_id)
);
create index on container(layer3domain_id, pool_id, network);

create table if not exists ips(
  layer3domain_id integer not null,
  version smallint not null,
  address decimal(40,0) not null,
  attributes jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now(),
  created_by varchar(128) not null,
  modified_at timestamptz not null default now(),
  modified_by varchar(128) not null,
  unique(layer3domain_id, version, address)
);

create table if not exists zones(
  id serial not null primary key,
  name varchar not null unique,
  attributes jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now(),
  created_by varchar(128) not null,
  modified_at timestamptz not null default now(),
  modified_by varchar(128) not null
);

create table if not exists zoneviews(
  id serial not null primary key,
  zone_id integer not null references zones(id),
  name varchar not null,
  serial bigint not null default 0,
  ttl integer not null default 3600,
  primary_name_server varchar not null,
  mail varchar not null,
  refresh integer not null default 10800,
  retry integer not null default 900,
  expire integer not null default 604800,
  minimum bigint not null default 86400,
  attributes jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now(),
  created_by varchar(128) not null,
  modified_at timestamptz not null default now(),
  modified_by varchar(128) not null
);
create index on zoneviews(zone_id, id);

create table if not exists records(
  zoneview_id integer not null references zoneviews(id) on delete cascade,
  name varchar(254) not null,
  type varchar(11) not null,
  ttl integer,
  value text not null,
  attributes jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now(),
  created_by varchar(128) not null,
  modified_at timestamptz not null default now(),
  modified_by varchar(128) not null,
  unique(zoneview_id, name, type, value)
);

create table if not exists outputgroups(
  id serial not null primary key,
  name varchar(128) not null unique,
  attributes jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now(),
  created_by varchar(128) not null,
  modified_at timestamptz not null default now(),
  modified_by varchar(128) not null
);

create table if not exists zoneviews_outputgroups(
  outputgroup_id integer not null references outputgroups(id),
  zoneview_id integer not null references zoneviews(id),
  unique(outputgroup_id, zoneview_id)
);

create table if not exists outputs(
  id serial not null primary key,
  name varchar(128) not null unique,
  plugin varchar(20) not null,
  db_uri varchar(250) not null,
  status varchar(250) not null,
  attributes jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now(),
  created_by varchar(128) not null,
  modified_at timestamptz not null default now(),
  modified_by varchar(128) not null
);

create table if not exists outputgroup_outputs(
  outputgroups_id integer not null references outputgroups(id),
  output_id integer not null references outputs(id),
  unique(outputgroups_id, output_id)
);
