-- ============================================================================
-- RAPPORT — Studio-Name aus Init in studio_settings übernehmen
-- ============================================================================
-- Bisher hat `create_studio_with_admin` nur `studios.name` und `studios.slug`
-- gesetzt. `studio_settings.name` wurde vom Seed-Trigger als Default
-- ("Mein Studio") angelegt — was im Frontend dann als Studio-Header und
-- Sidebar-Label erscheint. Discrepanz zur User-Eingabe.
--
-- Fix: nach Seed-Trigger den Studio-Namen in `studio_settings` schreiben und
-- `setup_completed = true` setzen (Frontend nutzt das für Setup-Wizard-Check).
-- ============================================================================

drop function if exists create_studio_with_admin(text, text, uuid[]);

create function create_studio_with_admin(
  p_name text,
  p_slug text,
  p_share_persons_from uuid[] default '{}'
)
  returns uuid
  language plpgsql
  security definer
as $$
declare
  v_studio_id uuid;
  v_user_id uuid := auth.uid();
  v_source_id uuid;
begin
  if v_user_id is null then
    raise exception 'Authentication required';
  end if;

  if array_length(p_share_persons_from, 1) > 0 then
    if exists (
      select 1 from unnest(p_share_persons_from) src
      where not exists (
        select 1 from studio_members sm
        where sm.user_id = v_user_id
          and sm.studio_id = src
          and sm.active = true
      )
    ) then
      raise exception 'You are not a member of all source studios';
    end if;
  end if;

  insert into studios (name, slug) values (p_name, p_slug) returning id into v_studio_id;
  insert into studio_members (studio_id, user_id, app_role_id)
    values (v_studio_id, v_user_id, 'r-admin');

  -- NEU: Studio-Name + setup_completed in die settings übernehmen, damit
  -- das Frontend nicht "Mein Studio" anzeigt und der Setup-Wizard nicht
  -- erneut triggert.
  update studio_settings
    set name = p_name, setup_completed = true
    where studio_id = v_studio_id;

  if array_length(p_share_persons_from, 1) > 0 then
    foreach v_source_id in array p_share_persons_from loop
      insert into person_studio_links (person_id, studio_id, primary_studio)
      select id, v_source_id, true
      from persons
      where studio_id = v_source_id
      on conflict (person_id, studio_id) do nothing;
      update persons set studio_id = NULL where studio_id = v_source_id;

      insert into person_studio_links (person_id, studio_id)
      select person_id, v_studio_id
      from person_studio_links
      where studio_id = v_source_id
      on conflict (person_id, studio_id) do nothing;
    end loop;
  end if;

  return v_studio_id;
end;
$$;

grant execute on function create_studio_with_admin(text, text, uuid[]) to authenticated;
