-- Generated canonical billing webhook sink seed.
-- Replace the client names or athena_base_url if you need environment-specific routing.

INSERT INTO athena.gateway_webhook_sink (
enabled,
name,
description,
athena_base_url,
client_name,
table_name,
route_key,
target_client_name,
target_table,
target_write_mode,
target_conflict_columns,
projection_template,
include_request_body_in_context,
lookup_keys,
slug,
state_key_template,
state_cap_max_fires,
state_cap_window_seconds,
idempotency_template,
state_resolution_mode,
state_resolution_query_builder
)
VALUES
(
    true,
    'Billing payments canonical mirror',
    'Mirrors billing payments emitted by provider webhook ingestion.',
    NULL,
    'athena-main',
    'billing.billing_payments',
    'billing_document_upserted',
    'athena-replica',
    'billing.billing_payments',
    'upsert',
    '["provider","provider_payment_id"]'::jsonb,
    '{
  "amount_currency": "{{ response.sql_write.row.amount_currency }}",
  "amount_refunded_currency": "{{ response.sql_write.row.amount_refunded_currency }}",
  "amount_refunded_value": "{{ response.sql_write.row.amount_refunded_value }}",
  "amount_value": "{{ response.sql_write.row.amount_value }}",
  "created_at": "{{ response.sql_write.row.created_at }}",
  "description": "{{ response.sql_write.row.description }}",
  "metadata": "{{ response.sql_write.row.metadata }}",
  "paid_at": "{{ response.sql_write.row.paid_at }}",
  "provider": "{{ response.sql_write.row.provider }}",
  "provider_customer_id": "{{ response.sql_write.row.provider_customer_id }}",
  "provider_payment_id": "{{ response.sql_write.row.provider_payment_id }}",
  "provider_payment_link_id": "{{ response.sql_write.row.provider_payment_link_id }}",
  "provider_profile_id": "{{ response.sql_write.row.provider_profile_id }}",
  "provider_subscription_id": "{{ response.sql_write.row.provider_subscription_id }}",
  "raw": "{{ response.sql_write.row.raw }}",
  "status": "{{ response.sql_write.row.status }}"
}'::jsonb,
    true,
    '{"provider_ref":"response:upsert.provider_ref","table":"response:upsert.table"}'::jsonb,
    'billing-payments-canonical-mirror',
    '{{ response.upsert.provider_ref }}|{{ response.sql_write.table }}',
    NULL,
    NULL,
    NULL,
    'template',
    '{}'::jsonb
),
(
    true,
    'Billing subscriptions canonical mirror',
    'Mirrors billing subscriptions emitted by provider webhook ingestion.',
    NULL,
    'athena-main',
    'billing.billing_subscriptions',
    'billing_document_upserted',
    'athena-replica',
    'billing.billing_subscriptions',
    'upsert',
    '["provider","provider_subscription_id"]'::jsonb,
    '{
  "amount_currency": "{{ response.sql_write.row.amount_currency }}",
  "amount_value": "{{ response.sql_write.row.amount_value }}",
  "canceled_at": "{{ response.sql_write.row.canceled_at }}",
  "created_at": "{{ response.sql_write.row.created_at }}",
  "description": "{{ response.sql_write.row.description }}",
  "interval": "{{ response.sql_write.row.interval }}",
  "metadata": "{{ response.sql_write.row.metadata }}",
  "next_payment_date": "{{ response.sql_write.row.next_payment_date }}",
  "provider": "{{ response.sql_write.row.provider }}",
  "provider_customer_id": "{{ response.sql_write.row.provider_customer_id }}",
  "provider_profile_id": "{{ response.sql_write.row.provider_profile_id }}",
  "provider_subscription_id": "{{ response.sql_write.row.provider_subscription_id }}",
  "raw": "{{ response.sql_write.row.raw }}",
  "status": "{{ response.sql_write.row.status }}"
}'::jsonb,
    true,
    '{"provider_ref":"response:upsert.provider_ref","table":"response:upsert.table"}'::jsonb,
    'billing-subscriptions-canonical-mirror',
    '{{ response.upsert.provider_ref }}|{{ response.sql_write.table }}',
    NULL,
    NULL,
    NULL,
    'template',
    '{}'::jsonb
),
(
    true,
    'Billing invoices canonical mirror',
    'Mirrors billing invoices emitted by provider webhook ingestion.',
    NULL,
    'athena-main',
    'billing.billing_invoices',
    'billing_document_upserted',
    'athena-replica',
    'billing.billing_invoices',
    'upsert',
    '["provider","provider_invoice_id"]'::jsonb,
    '{
  "amount_currency": "{{ response.sql_write.row.amount_currency }}",
  "amount_paid_currency": "{{ response.sql_write.row.amount_paid_currency }}",
  "amount_paid_value": "{{ response.sql_write.row.amount_paid_value }}",
  "amount_value": "{{ response.sql_write.row.amount_value }}",
  "created_at": "{{ response.sql_write.row.created_at }}",
  "description": "{{ response.sql_write.row.description }}",
  "issued_at": "{{ response.sql_write.row.issued_at }}",
  "metadata": "{{ response.sql_write.row.metadata }}",
  "paid_at": "{{ response.sql_write.row.paid_at }}",
  "provider": "{{ response.sql_write.row.provider }}",
  "provider_customer_id": "{{ response.sql_write.row.provider_customer_id }}",
  "provider_invoice_id": "{{ response.sql_write.row.provider_invoice_id }}",
  "provider_profile_id": "{{ response.sql_write.row.provider_profile_id }}",
  "raw": "{{ response.sql_write.row.raw }}",
  "status": "{{ response.sql_write.row.status }}"
}'::jsonb,
    true,
    '{"provider_ref":"response:upsert.provider_ref","table":"response:upsert.table"}'::jsonb,
    'billing-invoices-canonical-mirror',
    '{{ response.upsert.provider_ref }}|{{ response.sql_write.table }}',
    NULL,
    NULL,
    NULL,
    'template',
    '{}'::jsonb
)
ON CONFLICT (
    client_name,
    route_key,
    target_client_name,
    target_table,
    athena_base_url_norm,
    table_name_norm
)
DO UPDATE SET
    updated_at = now(),
    enabled = EXCLUDED.enabled,
    name = EXCLUDED.name,
    description = EXCLUDED.description,
    athena_base_url = EXCLUDED.athena_base_url,
    table_name = EXCLUDED.table_name,
    target_write_mode = EXCLUDED.target_write_mode,
    target_conflict_columns = EXCLUDED.target_conflict_columns,
    projection_template = EXCLUDED.projection_template,
    include_request_body_in_context = EXCLUDED.include_request_body_in_context,
    lookup_keys = EXCLUDED.lookup_keys,
    slug = COALESCE(EXCLUDED.slug, gateway_webhook_sink.slug),
    state_key_template = EXCLUDED.state_key_template,
    state_cap_max_fires = EXCLUDED.state_cap_max_fires,
    state_cap_window_seconds = EXCLUDED.state_cap_window_seconds,
    idempotency_template = EXCLUDED.idempotency_template,
    state_resolution_mode = EXCLUDED.state_resolution_mode,
    state_resolution_query_builder = EXCLUDED.state_resolution_query_builder;
