begin;
select plan(31); -- Total number of tests (reduced - removed idx_tablo_access_tablo_id tests)

-- ============================================================================
-- Tablo Access Indexes
-- ============================================================================

-- Note: idx_tablo_access_tablo_id does not exist in current schema
-- Only idx_tablo_access_user_id exists

SELECT has_index('public', 'tablo_access', 'idx_tablo_access_user_id',
    'Index on tablo_access.user_id should exist');

-- Test that the index is on the correct column
SELECT index_is_type('public', 'tablo_access', 'idx_tablo_access_user_id', 'btree',
    'tablo_access.user_id index should be btree');

-- ============================================================================
-- Events Table Indexes
-- ============================================================================

SELECT has_index('public', 'events', 'idx_events_tablo_id',
    'Index on events.tablo_id should exist');

SELECT has_index('public', 'events', 'idx_events_created_by',
    'Index on events.created_by should exist');

SELECT has_index('public', 'events', 'idx_events_start_date',
    'Index on events.start_date should exist');

SELECT has_index('public', 'events', 'idx_events_deleted_at',
    'Index on events.deleted_at should exist');

SELECT index_is_type('public', 'events', 'idx_events_tablo_id', 'btree',
    'events.tablo_id index should be btree');

SELECT index_is_type('public', 'events', 'idx_events_start_date', 'btree',
    'events.start_date index should be btree');

-- ============================================================================
-- Notes Table Indexes
-- ============================================================================

SELECT has_index('public', 'notes', 'idx_notes_user_id',
    'Index on notes.user_id should exist');

SELECT has_index('public', 'notes', 'idx_notes_deleted_at',
    'Index on notes.deleted_at should exist');

SELECT has_index('public', 'notes', 'idx_notes_created_at',
    'Index on notes.created_at should exist');

SELECT index_is_type('public', 'notes', 'idx_notes_user_id', 'btree',
    'notes.user_id index should be btree');

SELECT index_is_type('public', 'notes', 'idx_notes_deleted_at', 'btree',
    'notes.deleted_at index should be btree');

-- ============================================================================
-- Shared Notes Table Indexes
-- ============================================================================

SELECT has_index('public', 'shared_notes', 'idx_shared_notes_is_public',
    'Index on shared_notes.is_public should exist');

SELECT has_index('public', 'shared_notes', 'idx_shared_notes_user_id',
    'Index on shared_notes.user_id should exist');

SELECT index_is_type('public', 'shared_notes', 'idx_shared_notes_is_public', 'btree',
    'shared_notes.is_public index should be btree');

-- ============================================================================
-- Note Access Table Indexes
-- ============================================================================

SELECT has_index('public', 'note_access', 'idx_note_access_note_id',
    'Index on note_access.note_id should exist');

SELECT has_index('public', 'note_access', 'idx_note_access_user_id',
    'Index on note_access.user_id should exist');

SELECT has_index('public', 'note_access', 'idx_note_access_tablo_id',
    'Index on note_access.tablo_id should exist');

SELECT has_index('public', 'note_access', 'idx_note_access_is_active',
    'Index on note_access.is_active should exist');

-- ============================================================================
-- Unique Indexes for Note Access
-- ============================================================================

SELECT has_index('public', 'note_access', 'unique_note_access_with_tablo',
    'Unique index on note_access (note_id, user_id, tablo_id) should exist');

SELECT has_index('public', 'note_access', 'unique_note_access_all_tablos',
    'Unique index on note_access (note_id, user_id) for NULL tablo_id should exist');

-- ============================================================================
-- Primary Key Indexes
-- ============================================================================

-- Test that primary keys exist (which create implicit indexes)
SELECT has_pk('public', 'tablos', 'tablos should have primary key');
SELECT has_pk('public', 'tablo_access', 'tablo_access should have primary key');
SELECT has_pk('public', 'tablo_invites', 'tablo_invites should have primary key');
SELECT has_pk('public', 'feedbacks', 'feedbacks should have primary key');
SELECT has_pk('public', 'events', 'events should have primary key');
SELECT has_pk('public', 'notes', 'notes should have primary key');
SELECT has_pk('public', 'shared_notes', 'shared_notes should have primary key');
SELECT has_pk('public', 'note_access', 'note_access should have primary key');

-- ============================================================================
-- Verify Index Coverage for Common Query Patterns
-- ============================================================================

-- Test that commonly queried foreign key columns have indexes
-- This helps with JOIN performance and foreign key constraint enforcement

SELECT ok(
    (
        SELECT COUNT(*) 
        FROM pg_indexes 
        WHERE schemaname = 'public' 
        AND tablename = 'events'
        AND indexdef LIKE '%tablo_id%'
    ) > 0,
    'events should have index on tablo_id for foreign key joins'
);

select * from finish();
rollback;

