\connect sova_cabinet_test

DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
GRANT ALL ON SCHEMA public TO sova_test;

CREATE TABLE users (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    email VARCHAR(180),
    roles JSONB NOT NULL DEFAULT '[]',
    password VARCHAR(255),
    uid INTEGER NOT NULL UNIQUE,
    token VARCHAR(255),
    full_name VARCHAR(255) NOT NULL,
    phone VARCHAR(255),
    confirm BOOLEAN NOT NULL DEFAULT true,
    created_at TIMESTAMP DEFAULT now(),
    last_activity_at TIMESTAMP DEFAULT now()
);

CREATE TABLE city (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    region_id INTEGER NOT NULL,
    time_zone INTEGER NOT NULL DEFAULT 4
);

CREATE TABLE filial (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    fid INTEGER NOT NULL,
    name VARCHAR(255) NOT NULL,
    address VARCHAR(255) NOT NULL,
    address_name VARCHAR(255),
    site_id INTEGER,
    city_id INTEGER REFERENCES city(id) ON DELETE SET NULL,
    active BOOLEAN NOT NULL DEFAULT true,
    company VARCHAR(255)
);

CREATE TABLE banner (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    href VARCHAR(255) NOT NULL,
    src VARCHAR(255) NOT NULL,
    active BOOLEAN NOT NULL DEFAULT true,
    city_id INTEGER UNIQUE REFERENCES city(id) ON DELETE CASCADE
);

CREATE TABLE review_source (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    city_id INTEGER NOT NULL REFERENCES city(id) ON DELETE CASCADE,
    name VARCHAR(255) NOT NULL,
    count_row INTEGER NOT NULL DEFAULT 0,
    active BOOLEAN NOT NULL DEFAULT true,
    rating DOUBLE PRECISION NOT NULL DEFAULT 5,
    filial_id INTEGER REFERENCES filial(id) ON DELETE SET NULL,
    date_create DATE NOT NULL DEFAULT CURRENT_DATE
);

CREATE TABLE category_page (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    active BOOLEAN NOT NULL DEFAULT true
);

CREATE TABLE page (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    description TEXT NOT NULL,
    active BOOLEAN NOT NULL DEFAULT true,
    alias VARCHAR(255) NOT NULL,
    category_id INTEGER NOT NULL REFERENCES category_page(id) ON DELETE CASCADE
);

CREATE TABLE department (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    did INTEGER NOT NULL,
    name VARCHAR(255) NOT NULL,
    group_name VARCHAR(255) NOT NULL,
    online_mode BOOLEAN NOT NULL DEFAULT true,
    alias VARCHAR(255) NOT NULL,
    active BOOLEAN NOT NULL DEFAULT true,
    middle_name VARCHAR(255)
);

CREATE TABLE price_department (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    group_id INTEGER NOT NULL,
    group_name VARCHAR(255),
    doct_count INTEGER DEFAULT 0,
    view_in_web BOOLEAN DEFAULT true
);

CREATE TABLE price_list_view (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    kodoper INTEGER,
    schname VARCHAR(255),
    specname VARCHAR(255),
    speccode INTEGER,
    price_info JSONB,
    discpercent INTEGER,
    discprice NUMERIC(10,2),
    structname VARCHAR(255),
    fname VARCHAR(255),
    filial INTEGER,
    comment TEXT,
    media_id INTEGER,
    date_update TIMESTAMP DEFAULT now(),
    group_id INTEGER
);

CREATE TABLE price (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    name VARCHAR(255),
    value NUMERIC(10,2),
    property_value_id INTEGER,
    date_update TIMESTAMP DEFAULT now()
);

CREATE TABLE specialist_view (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    kinder INTEGER,
    speciality VARCHAR(255),
    category VARCHAR(255),
    experience VARCHAR(255),
    description TEXT,
    infoclinica BOOLEAN DEFAULT true,
    alias VARCHAR(255) NOT NULL,
    dcode INTEGER NOT NULL,
    s_type INTEGER,
    active BOOLEAN DEFAULT true,
    region_id INTEGER,
    kodoper JSONB,
    updated TIMESTAMP DEFAULT now(),
    accepts_dms BOOLEAN DEFAULT true,
    degree VARCHAR(255)
);

CREATE TABLE location_view (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    dcode INTEGER NOT NULL,
    department INTEGER NOT NULL,
    filial INTEGER NOT NULL,
    specialist_id INTEGER NOT NULL,
    online_mode BOOLEAN NOT NULL DEFAULT true,
    active BOOLEAN NOT NULL DEFAULT true,
    nearest_date DATE
);

CREATE TABLE remote_review (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    external_id INTEGER,
    specialist_id INTEGER,
    active BOOLEAN DEFAULT true,
    date_create DATE DEFAULT CURRENT_DATE,
    message TEXT,
    author VARCHAR(255),
    rating DOUBLE PRECISION DEFAULT 5,
    source VARCHAR(255)
);

CREATE TABLE record (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    specialist_id INTEGER NOT NULL,
    phone VARCHAR(255) NOT NULL,
    create_at TIMESTAMP NOT NULL DEFAULT now(),
    hash VARCHAR(255) NOT NULL,
    reserve JSONB NOT NULL DEFAULT '{}'
);

CREATE TABLE alert_sms (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    record_id INTEGER UNIQUE REFERENCES record(id) ON DELETE CASCADE,
    date_create TIMESTAMP NOT NULL DEFAULT now(),
    response TEXT NOT NULL
);

CREATE TABLE widget_form (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    name VARCHAR(255) NOT NULL
);

CREATE TABLE widget_form_input (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    text VARCHAR(255) NOT NULL,
    type VARCHAR(255) NOT NULL,
    bitrix24_id VARCHAR(255) NOT NULL,
    widget_form_id INTEGER REFERENCES widget_form(id) ON DELETE CASCADE,
    sort INTEGER NOT NULL DEFAULT 100
);

CREATE TABLE usrlog (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    pcode INTEGER,
    agent TEXT,
    client_ip VARCHAR(255),
    method VARCHAR(255),
    created_at TIMESTAMP DEFAULT now()
);

CREATE TABLE direct_company (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    name VARCHAR(255),
    company_id BIGINT,
    city VARCHAR(255)
);

CREATE TABLE direct_report (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    date DATE,
    ad_group_id BIGINT,
    campaign_id BIGINT,
    ad_id BIGINT,
    impressions INTEGER,
    clicks INTEGER,
    cost DOUBLE PRECISION,
    conversions INTEGER
);

