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

-- ============================================================================
-- Stripe Schema Tests
-- ============================================================================

SELECT has_schema('stripe', 'Stripe schema should exist');

-- ============================================================================
-- Stripe Function Existence Tests
-- ============================================================================

SELECT has_function('public', 'get_my_active_subscription',
    'Function get_my_active_subscription should exist');

SELECT has_function('public', 'get_user_stripe_customer',
    'Function get_user_stripe_customer should exist');

SELECT has_function('public', 'get_user_stripe_subscriptions',
    'Function get_user_stripe_subscriptions should exist');

SELECT has_function('public', 'get_stripe_products',
    'Function get_stripe_products should exist');

SELECT has_function('public', 'get_stripe_prices',
    'Function get_stripe_prices should exist');

SELECT has_function('public', 'is_paying_user', ARRAY['uuid'],
    'Function is_paying_user should exist with uuid parameter');

SELECT has_function('public', 'get_user_subscription_status', ARRAY['uuid'],
    'Function get_user_subscription_status should exist with uuid parameter');

SELECT has_function('public', 'get_user_stripe_customer_id', ARRAY['uuid'],
    'Function get_user_stripe_customer_id should exist with uuid parameter');

-- ============================================================================
-- Function Security Tests (SECURITY DEFINER)
-- ============================================================================

SELECT is(
    (
        SELECT prosecdef 
        FROM pg_proc 
        WHERE proname = 'get_my_active_subscription'
        LIMIT 1
    ),
    true,
    'get_my_active_subscription should be SECURITY DEFINER'
);

SELECT is(
    (
        SELECT prosecdef 
        FROM pg_proc 
        WHERE proname = 'get_user_stripe_customer'
        LIMIT 1
    ),
    true,
    'get_user_stripe_customer should be SECURITY DEFINER'
);

SELECT is(
    (
        SELECT prosecdef 
        FROM pg_proc 
        WHERE proname = 'get_user_stripe_subscriptions'
        LIMIT 1
    ),
    true,
    'get_user_stripe_subscriptions should be SECURITY DEFINER'
);

SELECT is(
    (
        SELECT prosecdef 
        FROM pg_proc 
        WHERE proname = 'get_stripe_products'
        LIMIT 1
    ),
    true,
    'get_stripe_products should be SECURITY DEFINER'
);

SELECT is(
    (
        SELECT prosecdef 
        FROM pg_proc 
        WHERE proname = 'get_stripe_prices'
        LIMIT 1
    ),
    true,
    'get_stripe_prices should be SECURITY DEFINER'
);

SELECT is(
    (
        SELECT prosecdef 
        FROM pg_proc 
        WHERE proname = 'is_paying_user'
        LIMIT 1
    ),
    true,
    'is_paying_user should be SECURITY DEFINER'
);

SELECT is(
    (
        SELECT prosecdef 
        FROM pg_proc 
        WHERE proname = 'get_user_subscription_status'
        LIMIT 1
    ),
    true,
    'get_user_subscription_status should be SECURITY DEFINER'
);

-- ============================================================================
-- Profile Stripe Columns Tests
-- ============================================================================

SELECT has_column('public', 'profiles', 'is_paying',
    'profiles should have is_paying column');

SELECT has_column('public', 'profiles', 'subscription_tier',
    'profiles should have subscription_tier column');

SELECT col_type_is('public', 'profiles', 'is_paying', 'boolean',
    'profiles.is_paying should be boolean');

SELECT col_type_is('public', 'profiles', 'subscription_tier', 'text',
    'profiles.subscription_tier should be text');

SELECT col_has_default('public', 'profiles', 'is_paying',
    'profiles.is_paying should have default value');

SELECT col_has_default('public', 'profiles', 'subscription_tier',
    'profiles.subscription_tier should have default value');

-- ============================================================================
-- Function Return Type Tests
-- ============================================================================

-- Test that is_paying_user returns boolean
SELECT is(
    (
        SELECT prorettype::regtype::text 
        FROM pg_proc 
        WHERE proname = 'is_paying_user'
        LIMIT 1
    ),
    'boolean',
    'is_paying_user should return boolean'
);

-- Test that get_user_stripe_customer_id returns text
SELECT is(
    (
        SELECT prorettype::regtype::text 
        FROM pg_proc 
        WHERE proname = 'get_user_stripe_customer_id'
        LIMIT 1
    ),
    'text',
    'get_user_stripe_customer_id should return text'
);

-- ============================================================================
-- Test Function Behavior
-- ============================================================================

-- Create test user for Stripe functions
DO $$
DECLARE
    stripe_user_id uuid := gen_random_uuid();
BEGIN
    -- Insert test user
    INSERT INTO auth.users (id, instance_id, aud, role, email, encrypted_password, email_confirmed_at, created_at, updated_at)
    VALUES 
        (stripe_user_id, '00000000-0000-0000-0000-000000000000', 'authenticated', 'authenticated', 'stripeuser@test.com', 'encrypted', now(), now(), now());
    
    -- Insert test profile
    INSERT INTO public.profiles (id, email, first_name, last_name, is_paying, subscription_tier)
    VALUES 
        (stripe_user_id, 'stripeuser@test.com', 'Stripe', 'User', false, 'free');
    
    -- Store test ID
    PERFORM set_config('test.stripe_user_id', stripe_user_id::text, true);
END $$;

-- Test: User has is_paying set to false by default
SELECT is(
    (
        SELECT is_paying 
        FROM public.profiles 
        WHERE id = current_setting('test.stripe_user_id')::uuid
        LIMIT 1
    ),
    false,
    'New user should have is_paying set to false'
);

-- Test: User has subscription_tier set to free by default
SELECT is(
    (
        SELECT subscription_tier 
        FROM public.profiles 
        WHERE id = current_setting('test.stripe_user_id')::uuid
        LIMIT 1
    ),
    'free',
    'New user should have subscription_tier set to free'
);

-- Test: is_paying_user returns false for non-paying user
SELECT is(
    public.is_paying_user(current_setting('test.stripe_user_id')::uuid),
    false,
    'is_paying_user should return false for user without active subscription'
);

-- Test: get_user_stripe_customer_id returns null for user without Stripe customer
SELECT is(
    public.get_user_stripe_customer_id(current_setting('test.stripe_user_id')::uuid),
    NULL,
    'get_user_stripe_customer_id should return null for user without Stripe customer'
);

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

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

-- Test that the view is secure (note: this view was replaced with a function in migration 37)
-- But we still test for its existence in case it's being used
SELECT ok(
    (SELECT COUNT(*) FROM information_schema.views WHERE table_schema = 'public' AND table_name = 'active_subscriptions') >= 0,
    'active_subscriptions view existence check'
);

-- ============================================================================
-- Subscription Plan Enum Tests (if exists)
-- ============================================================================

-- Check if subscription_plan type exists
SELECT ok(
    (SELECT COUNT(*) FROM pg_type WHERE typname = 'subscription_plan') >= 0,
    'Check for subscription_plan type'
);

-- ============================================================================
-- Comments and Documentation Tests
-- ============================================================================

-- Test that functions have comments for documentation
SELECT ok(
    (
        SELECT obj_description(oid) IS NOT NULL
        FROM pg_proc 
        WHERE proname = 'get_my_active_subscription'
        LIMIT 1
    ),
    'get_my_active_subscription should have documentation comment'
);

SELECT ok(
    (
        SELECT obj_description(oid) IS NOT NULL
        FROM pg_proc 
        WHERE proname = 'is_paying_user'
        LIMIT 1
    ),
    'is_paying_user should have documentation comment'
);

-- ============================================================================
-- Profile Update Tests
-- ============================================================================

-- Test updating a user's subscription status
DO $$
DECLARE
    paying_user_id uuid := gen_random_uuid();
BEGIN
    -- Insert test user
    INSERT INTO auth.users (id, instance_id, aud, role, email, encrypted_password, email_confirmed_at, created_at, updated_at)
    VALUES 
        (paying_user_id, '00000000-0000-0000-0000-000000000000', 'authenticated', 'authenticated', 'payinguser@test.com', 'encrypted', now(), now(), now());
    
    -- Insert test profile
    INSERT INTO public.profiles (id, email, first_name, last_name, is_paying, subscription_tier)
    VALUES 
        (paying_user_id, 'payinguser@test.com', 'Paying', 'User', false, 'free');
    
    -- Update to paying
    UPDATE public.profiles
    SET is_paying = true, subscription_tier = 'standard'
    WHERE id = paying_user_id;
    
    -- Store test ID
    PERFORM set_config('test.paying_user_id', paying_user_id::text, true);
END $$;

-- Test: Verify profile was updated to paying
SELECT is(
    (
        SELECT is_paying 
        FROM public.profiles 
        WHERE id = current_setting('test.paying_user_id')::uuid
        LIMIT 1
    ),
    true,
    'Profile should be updated to paying'
);

SELECT is(
    (
        SELECT subscription_tier 
        FROM public.profiles 
        WHERE id = current_setting('test.paying_user_id')::uuid
        LIMIT 1
    ),
    'standard',
    'Profile subscription_tier should be updated to standard'
);

select * from finish();
rollback;

