-- Enable required extensions
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- Create tables
CREATE TABLE IF NOT EXISTS files (
    id UUID NOT NULL PRIMARY KEY DEFAULT uuid_generate_v4(),
    file_type VARCHAR(15) 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',
    size FLOAT NOT NULL DEFAULT 10,
    created_at TIMESTAMP DEFAULT now()
    );
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 NOT NULL REFERENCES files(id) ON DELETE CASCADE,
    audio UUID NOT NULL 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 donats (
                                      id SERIAL PRIMARY KEY,
                                      streamer_id INTEGER NOT NULL,
                                      widget_id INTEGER NOT NULL,
                                      order_id TEXT NOT NULL,
                                      target_id INTEGER NOT NULL,
                                      text TEXT NOT NULL,
                                      amount INTEGER NOT NULL,
                                      donat_user TEXT NOT NULL,
                                      status VARCHAR(50) NOT NULL DEFAULT 'pending',
                                      created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
                                      updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);

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 donate_pages (
                                            id SERIAL PRIMARY KEY,
                                            streamer_login TEXT 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
);

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 INTEGER NOT NULL,
                                              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 function for updating timestamps
CREATE OR REPLACE FUNCTION update_updated_at()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-- Create triggers
CREATE TRIGGER update_updated_at_widgets
    BEFORE UPDATE ON widgets
    FOR EACH ROW
    EXECUTE FUNCTION update_updated_at();

CREATE TRIGGER update_updated_at_donats
    BEFORE UPDATE ON donats
    FOR EACH ROW
    EXECUTE FUNCTION update_updated_at();

CREATE TRIGGER update_updated_at_targets
    BEFORE UPDATE ON targets
    FOR EACH ROW
    EXECUTE FUNCTION 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'
       );