CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE TABLE IF NOT EXISTS files (
                                     id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    file_type VARCHAR(30) NOT NULL,
    file_name VARCHAR(50) NOT NULL,
    extension VARCHAR(10) NOT NULL,
    streamer_id INTEGER NOT NULL,
    entity VARCHAR(50) NOT NULL DEFAULT 'widget',
    created_at TIMESTAMP DEFAULT NOW(),
    size FLOAT NOT NULL DEFAULT 0
    );

CREATE TABLE IF NOT EXISTS widgets (
                                       id SERIAL PRIMARY KEY,
                                       streamer_id INTEGER NOT NULL,
                                       template_id INTEGER,
                                       group_id INTEGER NOT NULL DEFAULT 1,
                                       is_active BOOLEAN NOT NULL DEFAULT TRUE,
                                       name VARCHAR(50) NOT NULL,
    image UUID REFERENCES files(id) ON DELETE CASCADE,
    audio UUID REFERENCES files(id) ON DELETE CASCADE,
    duration INTEGER NOT NULL,
    min_amount INTEGER NOT NULL,
    max_amount INTEGER NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP NOT NULL DEFAULT NOW()
    );

CREATE TABLE IF NOT EXISTS targets (
                                       id SERIAL PRIMARY KEY,
                                       streamer_id INTEGER NOT NULL,
                                       text TEXT NOT NULL,
                                       amount INTEGER NOT NULL,
                                       collected INTEGER DEFAULT 0,
                                       created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
                                       updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS donats (
                                      id SERIAL PRIMARY KEY,
                                      streamer_id INTEGER NOT NULL,
                                      widget_id INTEGER REFERENCES widgets(id) NOT NULL,
    order_id UUID NOT NULL,
    target_id INTEGER REFERENCES targets(id),
    paid_time TIMESTAMP,
    status VARCHAR(50) NOT NULL DEFAULT 'pending',
    text TEXT NOT NULL,
    amount INTEGER NOT NULL,
    donat_user TEXT NOT NULL,
    accepted_time TIMESTAMP,
    rejected_time TIMESTAMP,
    show_name BOOLEAN NOT NULL DEFAULT TRUE,
    show_text BOOLEAN NOT NULL DEFAULT TRUE,
    play_content BOOLEAN NOT NULL DEFAULT TRUE,
    showed_time TIMESTAMP,
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
    );

CREATE TABLE IF NOT EXISTS donate_pages (
                                            id SERIAL PRIMARY KEY,
                                            streamer_login VARCHAR(255) NOT NULL,
    streamer_id INTEGER NOT NULL,
    description TEXT DEFAULT '',
    text_after_donat TEXT DEFAULT '',
    avatar UUID REFERENCES files(id) ON DELETE CASCADE,
    background_img UUID REFERENCES files(id) ON DELETE CASCADE,
    head_img UUID REFERENCES files(id) ON DELETE CASCADE
    );

CREATE TABLE IF NOT EXISTS moderation (
                                          id SERIAL PRIMARY KEY,
                                          streamer_id INTEGER NOT NULL,
                                          enable BOOLEAN DEFAULT FALSE,
                                          duration INTEGER NOT NULL DEFAULT 40
);

CREATE TABLE IF NOT EXISTS filters (
                                       id SERIAL PRIMARY KEY,
                                       streamer_id INTEGER NOT NULL,
                                       enable_links BOOLEAN DEFAULT FALSE
);

CREATE TABLE IF NOT EXISTS filters_words (
                                             id SERIAL PRIMARY KEY,
                                             donat_filter_id INTEGER NOT NULL REFERENCES filters(id),
    word TEXT NOT NULL
    );

CREATE TABLE IF NOT EXISTS voice_settings (
                                              id SERIAL PRIMARY KEY,
                                              streamer_id INTEGER NOT NULL,
                                              enable BOOLEAN DEFAULT FALSE,
                                              voice_speed VARCHAR(20) NOT NULL DEFAULT 'medium',
    scenery TEXT DEFAULT 'after_donat',
    voice_sound_percent INTEGER NOT NULL,
    min_price INTEGER NOT NULL
    );

CREATE TABLE IF NOT EXISTS languages (
                                         id SERIAL PRIMARY KEY,
                                         iso_code TEXT DEFAULT '',
                                         ru_name TEXT DEFAULT '',
                                         en_name TEXT DEFAULT ''
);

CREATE TABLE IF NOT EXISTS voices_languages (
                                                id SERIAL PRIMARY KEY,
                                                voice_setting_id INTEGER NOT NULL REFERENCES voice_settings(id),
    language_id INTEGER NOT NULL REFERENCES languages(id)
    );

CREATE TABLE IF NOT EXISTS streamers_widgets_pages (
                                                       id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    streamer_id INTEGER NOT NULL
    );

CREATE OR REPLACE FUNCTION update_updated_at()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER widgets_updated_at_trigger
    BEFORE UPDATE ON widgets
    FOR EACH ROW
    EXECUTE PROCEDURE update_updated_at();

CREATE TRIGGER donats_updated_at_trigger
    BEFORE UPDATE ON donats
    FOR EACH ROW
    EXECUTE PROCEDURE update_updated_at();

CREATE TRIGGER targets_updated_at_trigger
    BEFORE UPDATE ON targets
    FOR EACH ROW
    EXECUTE PROCEDURE update_updated_at();


-- Вставка default_avatar.png
INSERT INTO public.files (id, file_type, file_name, extension, streamer_id, size, entity)
VALUES (
           '25b2b94c-e5e6-49e4-9f40-d8814f632847',
           'image/png',
           'default_avatar.png',
           '.png',
           0,
           1024, -- Пример размера файла в байтах
           'donat_page' -- Опционально, если файл связан с donate_pages
       );

-- Вставка default_background.png
INSERT INTO public.files (id, file_type, file_name, extension, streamer_id, size, entity)
VALUES (
           '56527f54-87c0-4f4d-92b5-df6cb61b27d4',
           'image/png',
           'default_background.png',
           '.png',
           0,
           2048,
           'donat_page'
       );

-- Вставка default_head.png
INSERT INTO public.files (id, file_type, file_name, extension, streamer_id, size, entity)
VALUES (
           'fef345e0-ec9a-4c68-8f2a-dcccf660e640',
           'image/png',
           'default_head.png',
           '.png',
           0,
           512,
           'donat_page'
       );