create table if not exists "groups" (
    "name" varchar(20) not null,
    "comment" varchar(100) default null,
    constraint groups_pk primary key ("name") 
);

insert into "groups" ("name", "comment")
    values
        ('general', 'Общие контакты'),
        ('work', 'Рабочие контакты'),
        ('personal', 'Личные контакты');

create table if not exists lists (
    "name" varchar(20) not null,
    "comment" varchar(100) default null,
    constraint lists_pk primary key ("name") 
);

insert into lists ("name", "comment")
    values
        ('general', 'Общий список'),
        ('whitelist', 'Белый список'),
        ('blacklist', 'Черный список');

create table if not exists numbers (
    "number" varchar(12) not null,
    "group" varchar(20) not null default 'general',
    list varchar(20) not null default 'general',
    all_cc int not null default 0,
    white_cc int not null default 0,
    black_cc int not null default 0,  
    "comment" varchar(100) default null,
    constraint numbers_pk primary key ("number"),
    foreign key ("group") references "groups" ("name") on delete set null on update cascade,
    foreign key (list) references lists ("name") on delete set null on update cascade
);

create table if not exists sms (
    id bigserial not null,
    "date" timestamp not null default NOW(),
    "to" varchar(12) not null,
    "from" varchar(12) not null,
    "text" text not null,
    constraint sms_pk primary key (id)
);

create table if not exists ussd (
    id bigserial not null,
    "date" timestamp not null default NOW(),
    "to" varchar(12) not null,
    "type" smallint not null,
    "text" text not null,
    constraint ussd_pk primary key (id)
);

create table if not exists "server" (
    address varchar(50) not null,
    transparent_mode bool not null default false,
    internal_number varchar(12) not null,
    external_number varchar(12) not null,
    constraint server_pk primary key (address)
);
