-- IHF GAAF Compliance Foundation (IHUB-WP-0009)
-- T02: hub_kind on hubs
-- T03: type registries (widget_type, event_type, annotation_category, policy_scope) + seed
-- T04: maturity columns on existing contract tables
-- T05: hub_capability_manifests

-- ============================================================
-- T02 — Hub kind
-- ============================================================

ALTER TABLE hubs
    ADD COLUMN hub_kind TEXT NOT NULL DEFAULT 'domain';

-- 'framework' | 'domain' | 'shared'

CREATE INDEX hubs_hub_kind_idx ON hubs (hub_kind);

-- Only one framework hub at a time
CREATE UNIQUE INDEX hubs_one_framework_idx ON hubs (hub_kind)
    WHERE hub_kind = 'framework';

-- ============================================================
-- T03 — Type registries
-- ============================================================

-- widget_type_registry
CREATE TABLE widget_type_registry (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
    name TEXT NOT NULL UNIQUE,
    label TEXT NOT NULL,
    description TEXT,
    owner_hub_id UUID REFERENCES hubs(id),
    -- null = framework-level; non-null = domain-owned
    status TEXT NOT NULL DEFAULT 'active',
    -- 'active' | 'deprecated'
    deprecated_in_favour_of TEXT,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL
);

CREATE INDEX widget_type_registry_status_idx ON widget_type_registry (status);
CREATE INDEX widget_type_registry_owner_hub_idx ON widget_type_registry (owner_hub_id);

-- event_type_registry
CREATE TABLE event_type_registry (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
    name TEXT NOT NULL UNIQUE,
    label TEXT NOT NULL,
    description TEXT,
    owner_hub_id UUID REFERENCES hubs(id),
    status TEXT NOT NULL DEFAULT 'active',
    deprecated_in_favour_of TEXT,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL
);

CREATE INDEX event_type_registry_status_idx ON event_type_registry (status);
CREATE INDEX event_type_registry_owner_hub_idx ON event_type_registry (owner_hub_id);

-- annotation_category_registry
CREATE TABLE annotation_category_registry (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
    name TEXT NOT NULL UNIQUE,
    label TEXT NOT NULL,
    description TEXT,
    owner_hub_id UUID REFERENCES hubs(id),
    status TEXT NOT NULL DEFAULT 'active',
    deprecated_in_favour_of TEXT,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL
);

CREATE INDEX annotation_category_registry_status_idx ON annotation_category_registry (status);
CREATE INDEX annotation_category_registry_owner_hub_idx ON annotation_category_registry (owner_hub_id);

-- policy_scope_registry
CREATE TABLE policy_scope_registry (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
    name TEXT NOT NULL UNIQUE,
    label TEXT NOT NULL,
    description TEXT,
    owner_hub_id UUID REFERENCES hubs(id),
    status TEXT NOT NULL DEFAULT 'active',
    deprecated_in_favour_of TEXT,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL
);

CREATE INDEX policy_scope_registry_status_idx ON policy_scope_registry (status);
CREATE INDEX policy_scope_registry_owner_hub_idx ON policy_scope_registry (owner_hub_id);

-- ============================================================
-- T03 — Seed framework-level vocabulary (owner_hub_id = NULL)
-- ============================================================

INSERT INTO widget_type_registry (name, label, description) VALUES
    ('chart',         'Chart',              'Data visualisation chart widget'),
    ('form',          'Form',               'Data entry form widget'),
    ('table',         'Table',              'Tabular data display widget'),
    ('action',        'Action Control',     'Button, link, or trigger widget'),
    ('panel',         'Status Panel',       'Summary or status information panel'),
    ('workflow-step', 'Workflow Step',      'Single step in a multi-step workflow'),
    ('recommendation','Recommendation',     'AI or system recommendation block'),
    ('chat',          'Chat Region',        'Conversational interaction region'),
    ('diff',          'Diff / Review',      'Code diff or change review element');

INSERT INTO event_type_registry (name, label, description) VALUES
    ('viewed',                  'Viewed',                   'Widget was rendered and visible to the user'),
    ('focused',                 'Focused',                  'Widget received input focus'),
    ('clicked',                 'Clicked',                  'Widget was clicked or tapped'),
    ('submitted',               'Submitted',                'Form or action was submitted'),
    ('abandoned',               'Abandoned',                'User navigated away without completing'),
    ('retried',                 'Retried',                  'Action was retried after failure'),
    ('failed',                  'Failed',                   'Action or submission resulted in an error'),
    ('commented',               'Commented',                'User added a comment or annotation'),
    ('flagged_confusing',       'Flagged Confusing',        'User flagged the widget as confusing'),
    ('flagged_helpful',         'Flagged Helpful',          'User flagged the widget as helpful'),
    ('blocked_by_policy',       'Blocked by Policy',        'Action was blocked by a policy rule'),
    ('escalated',               'Escalated',                'Issue was escalated for review'),
    ('accepted_recommendation', 'Accepted Recommendation',  'User accepted an AI recommendation'),
    ('rejected_recommendation', 'Rejected Recommendation',  'User rejected an AI recommendation'),
    ('retracted',               'Retracted',                'Correction marker — references original event in metadata');

INSERT INTO annotation_category_registry (name, label, description) VALUES
    ('friction',             'Friction',              'Interaction caused user effort or difficulty'),
    ('missing_capability',   'Missing Capability',    'Required feature or function is absent'),
    ('policy_conflict',      'Policy Conflict',       'Widget behaviour conflicts with a policy'),
    ('trust_deficit',        'Trust Deficit',         'User lacks confidence in the widget output'),
    ('accessibility',        'Accessibility',         'Accessibility or inclusive design concern'),
    ('workflow_bottleneck',  'Workflow Bottleneck',   'Widget creates a slowdown in the workflow'),
    ('documentation_gap',    'Documentation Gap',     'Missing or insufficient documentation'),
    ('product_opportunity',  'Product Opportunity',   'Observation suggesting a product improvement'),
    ('governance_concern',   'Governance Concern',    'Concern about governance, audit, or compliance');

INSERT INTO policy_scope_registry (name, label, description) VALUES
    ('internal',    'Internal',              'Applies to internal operators only'),
    ('org-wide',    'Organisation-Wide',     'Applies across the entire organisation'),
    ('external',    'External-Facing',       'Applies to externally visible surfaces'),
    ('regulatory',  'Regulatory',            'Driven by regulatory or compliance requirements'),
    ('security',    'Security',              'Security policy scope');

-- ============================================================
-- T04 — Maturity columns on existing contract tables
-- ============================================================

ALTER TABLE envelope_emission_contracts
    ADD COLUMN maturity TEXT NOT NULL DEFAULT 'stable';
-- Existing v1.0 contract is stable

ALTER TABLE interaction_reporting_contracts
    ADD COLUMN maturity TEXT NOT NULL DEFAULT 'stable';
-- Existing v1.0 contract is stable

ALTER TABLE widget_adapter_specs
    ADD COLUMN maturity TEXT NOT NULL DEFAULT 'beta';
-- Existing adapter specs are beta until explicitly promoted

-- ============================================================
-- T05 — Hub Capability Manifest
-- ============================================================

CREATE TABLE hub_capability_manifests (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
    hub_id UUID NOT NULL UNIQUE REFERENCES hubs(id),
    manifest_version TEXT NOT NULL DEFAULT '1.0',
    declared_widget_types JSONB NOT NULL DEFAULT '[]',
    declared_event_types JSONB NOT NULL DEFAULT '[]',
    declared_annotation_categories JSONB NOT NULL DEFAULT '[]',
    declared_policy_scopes JSONB NOT NULL DEFAULT '[]',
    capability_description TEXT,
    contact TEXT,
    status TEXT NOT NULL DEFAULT 'draft',
    -- 'draft' | 'active' | 'retired'
    activated_at TIMESTAMP WITH TIME ZONE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL
);

CREATE INDEX hub_capability_manifests_hub_id_idx ON hub_capability_manifests (hub_id);
CREATE INDEX hub_capability_manifests_status_idx ON hub_capability_manifests (status);

-- ============================================================
-- GAAF marker: type registries enforced from here (IHUB-WP-0009)
-- All new type discriminator columns (widget_type, event_type, category,
-- policy_scope) must reference a registry table or carry a CHECK constraint.
-- ============================================================
