begin;
select plan(21); -- Total number of tests

-- ============================================================================
-- View Existence Tests
-- ============================================================================

SELECT has_view('public', 'user_tablos',
    'user_tablos view should exist');

SELECT has_view('public', 'active_subscriptions',
    'active_subscriptions view should exist');

-- ============================================================================
-- User Tablos View Tests
-- ============================================================================

-- Test that user_tablos view has expected columns
SELECT has_column('public', 'user_tablos', 'id',
    'user_tablos view should have id column');

SELECT has_column('public', 'user_tablos', 'user_id',
    'user_tablos view should have user_id column');

SELECT has_column('public', 'user_tablos', 'name',
    'user_tablos view should have name column');

SELECT has_column('public', 'user_tablos', 'status',
    'user_tablos view should have status column');

SELECT has_column('public', 'user_tablos', 'access_level',
    'user_tablos view should have access_level column');

SELECT has_column('public', 'user_tablos', 'is_admin',
    'user_tablos view should have is_admin column');

SELECT has_column('public', 'user_tablos', 'position',
    'user_tablos view should have position column');

SELECT has_column('public', 'user_tablos', 'deleted_at',
    'user_tablos view should have deleted_at column');

-- Test that user_tablos is defined with security_invoker
SELECT ok(
    (
        SELECT COUNT(*) 
        FROM pg_views 
        WHERE schemaname = 'public' 
        AND viewname = 'user_tablos'
        AND definition LIKE '%security_invoker%'
    ) > 0,
    'user_tablos view should use security_invoker'
);

-- ============================================================================
-- User Tablos View Behavior Tests
-- ============================================================================

-- Create test data for view testing
DO $$
DECLARE
    view_user1_id uuid := gen_random_uuid();
    view_user2_id uuid := gen_random_uuid();
    view_tablo1_id integer;
    view_tablo2_id integer;
BEGIN
    -- Insert test users
    INSERT INTO auth.users (id, instance_id, aud, role, email, encrypted_password, email_confirmed_at, created_at, updated_at)
    VALUES 
        (view_user1_id, '00000000-0000-0000-0000-000000000000', 'authenticated', 'authenticated', 'viewuser1@test.com', 'encrypted', now(), now(), now()),
        (view_user2_id, '00000000-0000-0000-0000-000000000000', 'authenticated', 'authenticated', 'viewuser2@test.com', 'encrypted', now(), now(), now());
    
    -- Insert test profiles
    INSERT INTO public.profiles (id, email, first_name, last_name)
    VALUES 
        (view_user1_id, 'viewuser1@test.com', 'View User', 'One'),
        (view_user2_id, 'viewuser2@test.com', 'View User', 'Two');
    
    -- Insert test tablos
    INSERT INTO public.tablos (owner_id, name, status, position)
    VALUES 
        (view_user1_id, 'View User 1 Tablo', 'todo', 0),
        (view_user2_id, 'View User 2 Tablo', 'in_progress', 1)
    RETURNING id INTO view_tablo1_id;
    
    -- Store test IDs
    PERFORM set_config('test.view_user1_id', view_user1_id::text, true);
    PERFORM set_config('test.view_user2_id', view_user2_id::text, true);
END $$;

-- Test: Verify user_tablos returns tablos for users
SELECT ok(
    (SELECT count(*) FROM public.user_tablos WHERE user_id = current_setting('test.view_user1_id')::uuid) > 0,
    'user_tablos should return tablos for user 1'
);

SELECT ok(
    (SELECT count(*) FROM public.user_tablos WHERE user_id = current_setting('test.view_user2_id')::uuid) > 0,
    'user_tablos should return tablos for user 2'
);

-- Test: Verify access_level is set correctly for owner
SELECT is(
    (
        SELECT access_level 
        FROM public.user_tablos 
        WHERE user_id = current_setting('test.view_user1_id')::uuid 
        AND name = 'View User 1 Tablo'
        LIMIT 1
    ),
    'admin',
    'Owner should have admin access_level in user_tablos view'
);

-- Test: Verify is_admin is true for owner
SELECT is(
    (
        SELECT is_admin 
        FROM public.user_tablos 
        WHERE user_id = current_setting('test.view_user1_id')::uuid 
        AND name = 'View User 1 Tablo'
        LIMIT 1
    ),
    true,
    'Owner should have is_admin true in user_tablos view'
);

-- Test: Verify deleted tablos are filtered out
SELECT is(
    (SELECT count(*) FROM public.user_tablos WHERE deleted_at IS NOT NULL),
    0::bigint,
    'user_tablos view should not return deleted tablos'
);

-- ============================================================================
-- Active Subscriptions View Tests
-- ============================================================================

-- Test that active_subscriptions view has expected columns
SELECT has_column('public', 'active_subscriptions', 'subscription_id',
    'active_subscriptions view should have subscription_id column');

SELECT has_column('public', 'active_subscriptions', 'user_id',
    'active_subscriptions view should have user_id column');

SELECT has_column('public', 'active_subscriptions', 'status',
    'active_subscriptions view should have status column');

-- ============================================================================
-- View Comments and Documentation
-- ============================================================================

-- Test that views have documentation comments
SELECT ok(
    (
        SELECT obj_description(c.oid) IS NOT NULL
        FROM pg_class c
        JOIN pg_namespace n ON n.oid = c.relnamespace
        WHERE n.nspname = 'public' 
        AND c.relname = 'user_tablos'
        AND c.relkind = 'v'
        LIMIT 1
    ),
    'user_tablos view should have documentation comment'
);

SELECT ok(
    (
        SELECT obj_description(c.oid) IS NOT NULL
        FROM pg_class c
        JOIN pg_namespace n ON n.oid = c.relnamespace
        WHERE n.nspname = 'public' 
        AND c.relname = 'active_subscriptions'
        AND c.relkind = 'v'
        LIMIT 1
    ),
    'active_subscriptions view should have documentation comment'
);

select * from finish();
rollback;

