begin;

create extension "basejump-supabase_test_helpers" version '0.0.6';

select no_plan();

-- =====================================================
-- Member Management Schema Tests
-- Verifies all tables, columns, and RLS settings
-- =====================================================

-- 1. Core tables exist
select has_table('public', 'members', 'members table exists');
select has_table('public', 'dues_categories', 'dues_categories table exists');
select has_table('public', 'membership_applications', 'membership_applications table exists');
select has_table('public', 'member_cards', 'member_cards table exists');
select has_table('public', 'member_departments', 'member_departments table exists');
select has_table('public', 'member_department_assignments', 'member_department_assignments table exists');
select has_table('public', 'member_roles', 'member_roles table exists');
select has_table('public', 'member_honors', 'member_honors table exists');
select has_table('public', 'sepa_mandates', 'sepa_mandates table exists');
select has_table('public', 'member_portal_invitations', 'member_portal_invitations table exists');
select has_table('public', 'member_transfers', 'member_transfers table exists');

-- 2. New Phase 1-4 tables exist
select has_table('public', 'member_audit_log', 'member_audit_log table exists');
select has_table('public', 'member_communications', 'member_communications table exists');
select has_table('public', 'member_tags', 'member_tags table exists');
select has_table('public', 'member_tag_assignments', 'member_tag_assignments table exists');
select has_table('public', 'member_merges', 'member_merges table exists');
select has_table('public', 'gdpr_retention_policies', 'gdpr_retention_policies table exists');
select has_table('public', 'member_notification_rules', 'member_notification_rules table exists');
select has_table('public', 'scheduled_job_configs', 'scheduled_job_configs table exists');
select has_table('public', 'scheduled_job_runs', 'scheduled_job_runs table exists');
select has_table('public', 'pending_member_notifications', 'pending_member_notifications table exists');

-- 3. New columns on members table
select has_column('public', 'members', 'primary_mandate_id', 'members has primary_mandate_id column');
select has_column('public', 'members', 'version', 'members has version column');

-- 4. New column on event_registrations
select has_column('public', 'event_registrations', 'member_id', 'event_registrations has member_id FK');

-- 5. RLS enabled on all member tables
select is(
  (select relrowsecurity from pg_class where relname = 'members' and relnamespace = 'public'::regnamespace),
  true, 'RLS enabled on members'
);
select is(
  (select relrowsecurity from pg_class where relname = 'member_audit_log' and relnamespace = 'public'::regnamespace),
  true, 'RLS enabled on member_audit_log'
);
select is(
  (select relrowsecurity from pg_class where relname = 'member_communications' and relnamespace = 'public'::regnamespace),
  true, 'RLS enabled on member_communications'
);
select is(
  (select relrowsecurity from pg_class where relname = 'member_tags' and relnamespace = 'public'::regnamespace),
  true, 'RLS enabled on member_tags'
);
select is(
  (select relrowsecurity from pg_class where relname = 'member_tag_assignments' and relnamespace = 'public'::regnamespace),
  true, 'RLS enabled on member_tag_assignments'
);
select is(
  (select relrowsecurity from pg_class where relname = 'member_notification_rules' and relnamespace = 'public'::regnamespace),
  true, 'RLS enabled on member_notification_rules'
);
select is(
  (select relrowsecurity from pg_class where relname = 'scheduled_job_configs' and relnamespace = 'public'::regnamespace),
  true, 'RLS enabled on scheduled_job_configs'
);

-- 6. Key indexes exist
select is(
  (select count(*) > 0 from pg_indexes where tablename = 'members' and indexname = 'ix_members_active_account_status'),
  true, 'Active members composite index exists'
);
select is(
  (select count(*) > 0 from pg_indexes where tablename = 'member_audit_log' and indexname = 'ix_member_audit_member'),
  true, 'Audit log member index exists'
);

-- 7. Check constraints exist on members
select is(
  (select count(*) > 0 from information_schema.check_constraints
   where constraint_name = 'chk_members_dob_not_future'),
  true, 'DOB not-future constraint exists'
);
select is(
  (select count(*) > 0 from information_schema.check_constraints
   where constraint_name = 'chk_members_exit_after_entry'),
  true, 'Exit-after-entry constraint exists'
);

-- 8. Version column has correct default
select is(
  (select column_default from information_schema.columns
   where table_name = 'members' and column_name = 'version'),
  '1', 'Version column defaults to 1'
);

select * from finish();

rollback;
