-- Member Registry Table (Event-sourced)
CREATE TABLE IF NOT EXISTS member_registry (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  member_did VARCHAR(255) NOT NULL UNIQUE,
  membership_class VARCHAR(50) NOT NULL,
  level_of_assurance VARCHAR(10) NOT NULL,
  resident_number VARCHAR(50),
  citizen_number VARCHAR(50),
  status VARCHAR(50) NOT NULL DEFAULT 'active',
  enrolled_at TIMESTAMP NOT NULL DEFAULT NOW(),
  expires_at TIMESTAMP,
  revoked_at TIMESTAMP,
  revocation_reason TEXT,
  metadata JSONB,
  created_at TIMESTAMP NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

-- Member Registry Events Table
CREATE TABLE IF NOT EXISTS member_registry_events (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  member_did VARCHAR(255) NOT NULL,
  event_type VARCHAR(50) NOT NULL,
  event_data JSONB NOT NULL,
  event_timestamp TIMESTAMP NOT NULL DEFAULT NOW(),
  created_by UUID,
  created_at TIMESTAMP NOT NULL DEFAULT NOW()
);

-- Member Registry Indexes
CREATE INDEX IF NOT EXISTS idx_member_registry_member_did ON member_registry(member_did);
CREATE INDEX IF NOT EXISTS idx_member_registry_membership_class ON member_registry(membership_class);
CREATE INDEX IF NOT EXISTS idx_member_registry_status ON member_registry(status);
CREATE INDEX IF NOT EXISTS idx_member_registry_resident_number ON member_registry(resident_number);
CREATE INDEX IF NOT EXISTS idx_member_registry_citizen_number ON member_registry(citizen_number);
CREATE INDEX IF NOT EXISTS idx_member_registry_events_member_did ON member_registry_events(member_did);
CREATE INDEX IF NOT EXISTS idx_member_registry_events_event_type ON member_registry_events(event_type);
CREATE INDEX IF NOT EXISTS idx_member_registry_events_event_timestamp ON member_registry_events(event_timestamp);

-- Good Standing Table
CREATE TABLE IF NOT EXISTS good_standing (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  member_did VARCHAR(255) NOT NULL,
  good_standing BOOLEAN NOT NULL DEFAULT true,
  verified_since TIMESTAMP NOT NULL DEFAULT NOW(),
  verified_until TIMESTAMP,
  compliance_checks JSONB,
  created_at TIMESTAMP NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

-- Good Standing Indexes
CREATE INDEX IF NOT EXISTS idx_good_standing_member_did ON good_standing(member_did);
CREATE INDEX IF NOT EXISTS idx_good_standing_good_standing ON good_standing(good_standing);

-- Service Contributions Table
CREATE TABLE IF NOT EXISTS service_contributions (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  member_did VARCHAR(255) NOT NULL,
  service_type VARCHAR(100) NOT NULL,
  hours DECIMAL(10, 2) NOT NULL,
  contribution_date DATE NOT NULL,
  verified_by UUID,
  verified_at TIMESTAMP,
  description TEXT,
  created_at TIMESTAMP NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

-- Service Contributions Indexes
CREATE INDEX IF NOT EXISTS idx_service_contributions_member_did ON service_contributions(member_did);
CREATE INDEX IF NOT EXISTS idx_service_contributions_contribution_date ON service_contributions(contribution_date);

