SET DEFINE OFF
WHENEVER SQLERROR EXIT SQL.SQLCODE

-- 009 · SEED — the PLATFORM-scope defaults of the Whitelabel Catalogue § 5 / § 5b as published POLICY_VALUES rows.
-- Customer-scope values (Catalogue § 1–§ 4, § 6) are seeded by the customer plug-in (AI.Support.Plugin.<Customer>) at
-- first start, not here. MERGE = twice-runnable; a changed default is a NEW version row, never an edit (the old row is retired).
-- Every VALUE is JSON. Durations are ISO-8601 (PT15M = 15 minutes, P7D = 7 days).

DECLARE
    PROCEDURE seed(p_key IN VARCHAR2, p_type IN VARCHAR2, p_value IN VARCHAR2, p_section IN VARCHAR2) IS
    BEGIN
        MERGE INTO "SRD_SUPPORT"."POLICY_VALUES" t
        USING (SELECT 'platform' AS scope, p_key AS k, 1 AS v FROM DUAL) s
           ON (t."SCOPE" = s.scope AND t."CUSTOMER_CODE" = '*' AND t."MODULE" = '*' AND t."CASE_TYPE" = '*' AND t."PROFILE_KEY" = '*' AND t."KEY" = s.k AND t."VERSION" = s.v)
        WHEN NOT MATCHED THEN INSERT ("ID", "SCOPE", "KEY", "VERSION", "VALUE", "VALUE_TYPE", "CATALOGUE_SECTION", "STATE", "SET_BY", "PUBLISHED_ON_UTC", "CREATED_ON_UTC")
             VALUES ('policy:' || p_key || ':1', 'platform', p_key, 1, p_value, p_type, p_section, 'published', 'seed 009', SYS_EXTRACT_UTC(SYSTIMESTAMP), SYS_EXTRACT_UTC(SYSTIMESTAMP));
    END;
BEGIN
    -- § 5 Runtime, budgets and thresholds — budget is TIME only (D135)
    seed('budget.dimension',                 'string',   '"minutes"',  '§ 5');
    seed('budget.soft_warning',              'number',   '80',         '§ 5');
    seed('budget.hard_stop',                 'number',   '100',        '§ 5');
    seed('budget.per_role',                  'json',     '{"root":10,"stage":60,"sub_agent":"profile","write_executor":"profile","consults":10}', '§ 5');
    seed('budget.max_concurrent_subagents',  'number',   '4',          '§ 5');
    seed('budget.db_sessions_per_env',       'number',   '1',          '§ 5');
    seed('budget.consult_concurrency',       'number',   '2',          '§ 5');
    seed('escalation.failed_attempts',       'number',   '2',          '§ 5');
    seed('compaction.threshold',             'number',   '80',         '§ 5');
    seed('compaction.retain_turns',          'number',   '6',          '§ 5');
    seed('compaction.tool_result_prune',     'json',     '{"over_chars":8000,"head_chars":4000,"tail_chars":1000}', '§ 5');
    seed('verifier.tiers',                   'json',     '{"mandatory":["customer_facing_mechanism","memory_write","first_three_of_shape"],"sampled_one_in":5}', '§ 5');
    seed('write_auditor.tier',               'string',   '"mandatory"', '§ 5');
    seed('precipitation.ladder',             'json',     '[3,7,20]',   '§ 5');
    seed('held_batches.preflight_sampling',  'json',     '{"ordered":"ordinal","unordered":"per_item"}', '§ 5');
    seed('held_batches.repreflight_after',   'duration', '"P7D"',      '§ 5');
    seed('grants.expiry.write',              'duration', '"PT4H"',     '§ 5');
    seed('grants.expiry.read',               'duration', '"P7D"',      '§ 5');
    seed('grants.iteration_window',          'json',     '{"same_object_set":true,"same_day":true}', '§ 5');
    seed('writes.shape_expiry_default',      'duration', '"P90D"',     '§ 5');
    seed('writes.shape_extension_max',       'duration', '"P1D"',      '§ 5');
    seed('writes.always_human',              'json',     '["W3","W4","W5","W6","W7"]', '§ 1');
    seed('writes.auto_confirm_classes',      'json',     '["W1","W2"]', '§ 1');
    seed('writes.clean_instances_required',  'number',   '3',          '§ 5');
    seed('ledger.seal_interval',             'duration', '"PT15M"',    '§ 5');
    seed('ledger.export_interval',           'duration', '"PT15M"',    '§ 5');
    seed('ledger.genesis_hash',              'string',   '"0000000000000000000000000000000000000000000000000000000000000000"', '§ 5');
    seed('ledger.canonical_form',            'string',   '"SERDICA-JCS-1"', '§ 5');
    seed('roi.unit',                         'string',   '"hours"',    '§ 5');
    seed('roi.switch_cost_share',            'number',   '20',         '§ 5');
    seed('roi.agent_time_adjustment',        'number',   '-25',        '§ 5');
    seed('roi.human_time_adjustment',        'number',   '25',         '§ 5');
    seed('roi.human_minutes_per_ticket',     'number',   '30',         '§ 5');
    seed('nfr.targets',                      'json',     '{"concurrent_cases":20,"concurrent_cases_ceiling":50,"gate_roundtrip_ms":2000,"transcript_lag_ms":5000,"ledger_rpo":"PT15M","case_rpo":"PT1H","rto":"PT4H","restore_test":"quarterly"}', '§ 5');
    seed('kill_switch.modes',                'json',     '["soft","hard"]', '§ 5');
    seed('kill_switch.may_flip',             'json',     '["administrator"]', '§ 5');
    seed('eval.batching',                    'json',     '{"per":"domain","schedule":"weekly","before_publish":true}', '§ 5');
    seed('memory.promotion_rule',            'json',     '{"states":["case_local","proposed","active","retired"],"confirmation":["human_review","two_uses"],"two_uses_min_cases":2}', '§ 5');
    seed('summarizer.node_cap',              'json',     '{"lines":300,"articles":5}', '§ 5');
    seed('summarizer.refresh',               'json',     '{"commits":50,"days":90}', '§ 5');
    seed('metrics.rebaseline_after_cases',   'number',   '10',         '§ 5');
    seed('ui.transcript',                    'json',     '{"page_size":200,"max_notifications_per_s":10,"payloads":"reference"}', '§ 5');
    seed('gates.escalation_levels',          'json',     '["holders","all_holders","administrator"]', '§ 5');

    -- § 5b Inter-module traffic
    seed('consults.depth_max',               'number',   '1',          '§ 5b');
    seed('consults.budget_share',            'number',   '10',         '§ 5b');
    seed('consults.deadline.memory_only',    'duration', '"PT10M"',    '§ 5b');
    seed('consults.deadline.with_reads',     'duration', '"PT30M"',    '§ 5b');
    seed('handovers.directions',             'json',     '["support->configuration","support->source","configuration->source","source->configuration"]', '§ 5b');
    seed('intake.low_confidence_flag',       'boolean',  'true',       '§ 5b');

    -- § 4 / § 10.3 reach — direct connections only (D132); the tunnel route kind is dropped
    seed('connectors.route_kinds',           'json',     '["direct","unreachable"]', '§ 4');

    -- contracts — the inter-stage contracts are drafts (D139): the platform validates the required core and accepts extensions
    seed('contracts.inter_stage.mode',       'string',   '"draft"',    'contracts');
    seed('contracts.inter_stage.additional_properties', 'boolean', 'true', 'contracts');
    COMMIT;
END;
/
