-- Клипы
CREATE TABLE clips (
                       id SERIAL PRIMARY KEY,
                       title VARCHAR(255) NOT NULL,
                       video_url TEXT NOT NULL,
                       thumbnail_url TEXT NOT NULL,
                       duration INT NOT NULL,
                       author_id INTEGER NOT NULL,
                       likes_count INTEGER DEFAULT 0,
                       comments_count INTEGER DEFAULT 0,
                       created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
                       updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
                       deleted_at TIMESTAMP WITH TIME ZONE
);

-- Лайки клипов
CREATE TABLE clip_likes (
                            id SERIAL PRIMARY KEY,
                            clip_id INTEGER NOT NULL REFERENCES clips(id) ON DELETE CASCADE,
                            user_id INTEGER NOT NULL,
                            created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
                            UNIQUE(clip_id, user_id)
);

-- Комментарии к клипам
CREATE TABLE clip_comments (
                               id SERIAL PRIMARY KEY,
                               content TEXT NOT NULL,
                               clip_id INTEGER NOT NULL REFERENCES clips(id) ON DELETE CASCADE,
                               author_id INTEGER NOT NULL,
                               created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
                               updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
                               deleted_at TIMESTAMP WITH TIME ZONE
);

-- Индексы для производительности
CREATE INDEX idx_clips_author_id ON clips(author_id);
CREATE INDEX idx_clip_likes_clip_id ON clip_likes(clip_id);
CREATE INDEX idx_clip_likes_user_id ON clip_likes(user_id);
CREATE INDEX idx_clip_comments_clip_id ON clip_comments(clip_id);
CREATE INDEX idx_clip_comments_author_id ON clip_comments(author_id);
CREATE INDEX idx_clips_deleted_at ON clips(deleted_at);
CREATE INDEX idx_clip_comments_deleted_at ON clip_comments(deleted_at);