\connect sova_backend_test

INSERT INTO users (uid, email, roles, region_id, password, birth_date)
VALUES (100001, '8e6ec50bec697ce8e2663b4790264408', '["ROLE_USER","ROLE_ADMIN"]', 91, '$2y$13$Y98kYF03yQjzDo0HsvvWMu..4SqB3Kgv2d8GNf870MEjJbXUJ2i.a', '1990-01-01');

INSERT INTO departments (did, name, online_mode, alias, group_name)
VALUES (10, 'Терапия', true, 'terapiya', 'Взрослые специалисты');

INSERT INTO filial (fid, name, address, region_id, site_id, company, short_name, phone, email)
VALUES
    (1, 'Сова Саратов', 'Localhost, 1', 91, 1, 'ООО Local Clinic', 'Саратов', '+70000000001', 'saratov@example.test'),
    (2, 'Сова Волгоград', 'Localhost, 2', 92, 2, 'ООО Local Clinic', 'Волгоград', '+70000000002', 'volgograd@example.test'),
    (3, 'Сова Воронеж', 'Localhost, 3', 93, 3, 'ООО Local Clinic', 'Воронеж', '+70000000003', 'voronezh@example.test'),
    (4, 'Сова Краснодар', 'Localhost, 4', 94, 4, 'ООО Local Clinic', 'Краснодар', '+70000000004', 'krasnodar@example.test');

INSERT INTO specialist (name, active, display_schedule, dcodes, region_id, alias, post, experience, s_type, anons, content, kodoper, filials, accepts_dms, specialities)
VALUES ('Иванов Иван Иванович', true, true, '101', 91, 'ivanov-ivan', 'Врач-терапевт', '2014', 1, 'Тестовый врач', 'Тестовое описание врача', '[1001]', '[1]', true, '["Терапия"]');

INSERT INTO location (dcode, department, filial, online_mode, active, nearest_date, specialist_id)
VALUES (101, 10, 1, true, true, CURRENT_DATE + interval '1 day', 1);

INSERT INTO review (specialist_id, message, author, rating, source, external_id)
VALUES (1, 'Тестовый отзыв для локальной базы', 'Пациент Local', 5, 'local', 1);

INSERT INTO specialist_docs (specialist_id, name, active, type)
VALUES (1, 'Тестовый сертификат', true, 'certificate');

INSERT INTO specialist_dcode_description (dcode, department, specialist_id, content)
VALUES (101, 10, 1, 'Локальное описание врача по dcode.');

INSERT INTO stock (name, content, anons)
VALUES ('Local акция', 'Тестовая акция только для локальной разработки', 'Local');

INSERT INTO stock_specialist (stock_id, specialist_id) VALUES (1, 1);

INSERT INTO price_department (name, group_id, doct_count, view_in_web)
VALUES ('Консультации', 100, 1, true);

INSERT INTO price_list (kodoper, schname, specname, speccode, price_info, discprice, structname, fname, filial, group_id)
VALUES (1001, 'Прием терапевта', 'Иванов Иван Иванович', 101, '{"base":1500,"discount":1200}', 1200, 'Терапия', 'Сова Local', 1, 100);

INSERT INTO schedule (dcode, department, filial, schedident, workdate, time, is_free, online_mode, price_info)
VALUES (101, 10, 1, 555001, CURRENT_DATE + interval '1 day', '[{"st":"09:00","en":"09:30"}]', true, true, 1500);

INSERT INTO record (specialist_id, phone, hash, reserve)
VALUES (1, '+70000000000', 'c2018e2ee0f7d39af2c8f696e7003424', '{"local":true,"date":"tomorrow"}');

INSERT INTO alert_sms (record_id, response)
VALUES (1, '{"status":"local-ok"}');

INSERT INTO widget_form (name) VALUES ('Local форма');
INSERT INTO widget_form_input (text, type, bitrix24_id, widget_form_id, sort)
VALUES ('Телефон', 'phone', 'PHONE', 1, 10);

INSERT INTO article (name, active, region_id, alias, anons, content)
VALUES ('Local статья', true, 91, 'local-article', 'Анонс', 'Контент локальной статьи');

INSERT INTO disease (name, active, region_id, alias, anons, content)
VALUES ('Local заболевание', true, 91, 'local-disease', 'Анонс', 'Описание');

INSERT INTO medical_center (name, active, region_id, alias, anons, content)
VALUES ('Local медцентр', true, 91, 'local-center', 'Анонс', 'Описание');

INSERT INTO news (name, active, region_id, alias, anons, content)
VALUES ('Local новость', true, 91, 'local-news', 'Анонс', 'Контент');

INSERT INTO promo (name, active, region_id, alias, anons, content, period)
VALUES ('Local промо', true, 91, 'local-promo', 'Анонс', 'Контент', 'Всегда');

INSERT INTO site_services (name, active, region_id, alias, anons, content, tags)
VALUES ('Local услуга', true, 91, 'local-service', 'Анонс', 'Контент', '["local"]');

INSERT INTO web_get_docinfo (source_table, doc_name, doc_post, filial, viewinweb, depnum, accepts_dms, anons, content)
VALUES ('local', 'Иванов Иван Иванович', 'Врач-терапевт', 1, true, 10, true, 'Анонс', 'Описание');

INSERT INTO idoctor (dcode, name, department, filial, nearest_date, online_mode)
VALUES (101, 'Иванов Иван Иванович', 10, 1, CURRENT_DATE + interval '1 day', true);

-- После seed INSERT sequence identity должен указывать на MAX(id), иначе CREATE вернёт duplicate key id=1.
DO $body$
DECLARE
    tbl text;
BEGIN
    FOREACH tbl IN ARRAY ARRAY[
        'users', 'departments', 'filial', 'specialist', 'location', 'review',
        'specialist_docs', 'specialist_dcode_description', 'stock',
        'price_department', 'price_list', 'schedule', 'record', 'alert_sms',
        'widget_form', 'widget_form_input', 'article', 'disease', 'medical_center',
        'news', 'promo', 'site_services', 'web_get_docinfo', 'idoctor'
    ]
    LOOP
        EXECUTE format(
            'SELECT setval(pg_get_serial_sequence(%L, ''id''), COALESCE((SELECT MAX(id) FROM %I), 1), true)',
            tbl,
            tbl
        );
    END LOOP;
END
$body$;
