-- +goose Up
-- +goose StatementBegin

-- =========================================
-- ДОПОЛНИТЕЛЬНЫЕ ИНДЕКСЫ ДЛЯ ОПТИМИЗАЦИИ
-- =========================================

-- Индексы для users
CREATE INDEX idx_users_volunteer_rating ON users(volunteer_rating DESC) WHERE volunteer_rating > 0 AND deleted_at IS NULL;
CREATE INDEX idx_users_completed_requests ON users(completed_requests_count DESC) WHERE completed_requests_count > 0 AND deleted_at IS NULL;
CREATE INDEX idx_users_created_at ON users(created_at DESC);

-- Составные индексы для requests (для сложных запросов)
CREATE INDEX idx_requests_status_created ON requests(status, created_at DESC) WHERE deleted_at IS NULL;
CREATE INDEX idx_requests_requester_status ON requests(requester_id, status) WHERE deleted_at IS NULL;
CREATE INDEX idx_requests_volunteer_status ON requests(assigned_volunteer_id, status) WHERE assigned_volunteer_id IS NOT NULL;
CREATE INDEX idx_requests_type_status ON requests(request_type_id, status) WHERE deleted_at IS NULL;

-- Индексы для volunteer_responses
CREATE INDEX idx_volunteer_responses_created_at ON volunteer_responses(created_at DESC);
CREATE INDEX idx_volunteer_responses_request_status ON volunteer_responses(request_id, status);

-- Индексы для ratings
CREATE INDEX idx_ratings_volunteer_rating ON ratings(volunteer_id, rating DESC);

-- Индексы для complaints
CREATE INDEX idx_complaints_status_created ON complaints(status, created_at DESC);
CREATE INDEX idx_complaints_defendant_status ON complaints(defendant_id, status);

-- Индексы для user_blocks
CREATE INDEX idx_user_blocks_blocked_until ON user_blocks(blocked_until) WHERE is_active = TRUE AND blocked_until IS NOT NULL;

-- Полнотекстовый поиск для requests
CREATE INDEX idx_requests_title_trgm ON requests USING gin(title gin_trgm_ops);
CREATE INDEX idx_requests_description_trgm ON requests USING gin(description gin_trgm_ops);

-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin

DROP INDEX IF EXISTS idx_requests_description_trgm;
DROP INDEX IF EXISTS idx_requests_title_trgm;
DROP INDEX IF EXISTS idx_user_blocks_blocked_until;
DROP INDEX IF EXISTS idx_complaints_defendant_status;
DROP INDEX IF EXISTS idx_complaints_status_created;
DROP INDEX IF EXISTS idx_ratings_volunteer_rating;
DROP INDEX IF EXISTS idx_volunteer_responses_request_status;
DROP INDEX IF EXISTS idx_volunteer_responses_created_at;
DROP INDEX IF EXISTS idx_requests_type_status;
DROP INDEX IF EXISTS idx_requests_volunteer_status;
DROP INDEX IF EXISTS idx_requests_requester_status;
DROP INDEX IF EXISTS idx_requests_status_created;
DROP INDEX IF EXISTS idx_users_created_at;
DROP INDEX IF EXISTS idx_users_completed_requests;
DROP INDEX IF EXISTS idx_users_volunteer_rating;

-- +goose StatementEnd
