-- Insert/update feedgen with CTE to resolve actor_id
-- Self-contained schema: No records table, metadata embedded directly
-- Parameters: $1=actor_id, $2=rkey, $3=cid, $4=service_actor_id, $5=content_mode, $6=name,
--             $7=description, $8=description_facets, $9=avatar_cid, $10=created_at
INSERT INTO feedgens (
    actor_id, rkey, cid, created_at, owner_actor_id, service_actor_id, content_mode, name,
    description, description_facets, avatar_cid, accepts_interactions
)
SELECT
    $1,  -- actor_id (provided by caller)
    $2,  -- rkey (TEXT, provided by caller)
    $3::bytea,  -- cid (embedded)
    $10,  -- created_at (embedded)
    $1,  -- owner_actor_id (same as actor_id for feed generators)
    $4,  -- service_actor_id (passed as parameter, already ensured in executor)
    $5::text::content_mode,
    $6,  -- name
    $7,  -- description
    $8,  -- description_facets
    $9,  -- avatar_cid
    true  -- Default accepts_interactions to true
ON CONFLICT (actor_id, rkey) DO UPDATE SET
    cid=EXCLUDED.cid,
    service_actor_id=EXCLUDED.service_actor_id,
    content_mode=EXCLUDED.content_mode,
    name=EXCLUDED.name,
    description=EXCLUDED.description,
    description_facets=EXCLUDED.description_facets,
    avatar_cid=EXCLUDED.avatar_cid,
    status='complete'::feedgen_status
RETURNING XMAX::text::int
