begin;
select plan(17);

-- ============================================================================
-- Table Existence
-- ============================================================================

SELECT has_table('public', 'apple_customers', 'apple_customers table should exist');
SELECT has_table('public', 'apple_subscriptions', 'apple_subscriptions table should exist');
SELECT has_table(
  'public',
  'apple_subscription_events',
  'apple_subscription_events table should exist'
);

-- ============================================================================
-- Column Coverage
-- ============================================================================

SELECT has_column('public', 'apple_customers', 'user_id', 'apple_customers should have user_id');
SELECT has_column(
  'public',
  'apple_customers',
  'revenuecat_app_user_id',
  'apple_customers should have revenuecat_app_user_id'
);
SELECT has_column(
  'public',
  'apple_subscriptions',
  'owner_user_id',
  'apple_subscriptions should have owner_user_id'
);
SELECT has_column(
  'public',
  'apple_subscriptions',
  'original_transaction_id',
  'apple_subscriptions should have original_transaction_id'
);
SELECT has_column(
  'public',
  'apple_subscription_events',
  'event_id',
  'apple_subscription_events should have event_id'
);

-- ============================================================================
-- Primary Keys + Constraints
-- ============================================================================

SELECT has_pk('public', 'apple_customers', 'apple_customers should have primary key');
SELECT has_pk('public', 'apple_subscriptions', 'apple_subscriptions should have primary key');
SELECT has_pk(
  'public',
  'apple_subscription_events',
  'apple_subscription_events should have primary key'
);

SELECT has_index(
  'public',
  'apple_customers',
  'apple_customers_user_id_key',
  'apple_customers.user_id unique constraint should exist'
);
SELECT has_index(
  'public',
  'apple_customers',
  'apple_customers_revenuecat_app_user_id_key',
  'apple_customers.revenuecat_app_user_id unique constraint should exist'
);
SELECT has_index(
  'public',
  'apple_subscriptions',
  'apple_subscriptions_original_transaction_id_key',
  'apple_subscriptions.original_transaction_id unique constraint should exist'
);

-- ============================================================================
-- Performance Indexes
-- ============================================================================

SELECT has_index(
  'public',
  'apple_subscriptions',
  'idx_apple_subscriptions_owner_user_id',
  'apple_subscriptions owner_user_id index should exist'
);
SELECT has_index(
  'public',
  'apple_subscriptions',
  'idx_apple_subscriptions_status',
  'apple_subscriptions status index should exist'
);
SELECT has_index(
  'public',
  'apple_subscriptions',
  'idx_apple_subscriptions_current_period_end',
  'apple_subscriptions current_period_end index should exist'
);

select * from finish();
rollback;
