-- Store temporary signup codes and verify them through server-only RPCs.

create table if not exists public.email_verification_codes (
  user_id uuid primary key references auth.users(id) on delete cascade,
  email citext not null,
  code_hash text not null,
  expires_at timestamptz not null,
  attempts smallint not null default 0 check (attempts between 0 and 5),
  sent_at timestamptz not null default now(),
  window_started_at timestamptz not null default now(),
  send_count smallint not null default 1 check (send_count between 1 and 5)
);

alter table public.email_verification_codes enable row level security;
revoke all on table public.email_verification_codes from public, anon, authenticated;
grant all on table public.email_verification_codes to service_role;

create or replace function public.verify_signup_email_code(
  target_user_id uuid,
  submitted_code_hash text
)
returns text
language plpgsql
security definer
set search_path = public, pg_temp
as $$
declare
  stored public.email_verification_codes%rowtype;
begin
  select * into stored
  from public.email_verification_codes
  where user_id = target_user_id
  for update;

  if not found then
    return 'missing';
  end if;
  if stored.expires_at <= now() then
    return 'expired';
  end if;
  if stored.attempts >= 5 then
    return 'too_many_attempts';
  end if;
  if stored.code_hash = submitted_code_hash then
    return 'verified';
  end if;

  update public.email_verification_codes
  set attempts = attempts + 1
  where user_id = target_user_id;
  return case when stored.attempts + 1 >= 5 then 'too_many_attempts' else 'invalid' end;
end;
$$;

create or replace function public.issue_signup_email_code(
  target_user_id uuid,
  submitted_code_hash text,
  new_expires_at timestamptz
)
returns text
language plpgsql
security definer
set search_path = public, pg_temp
as $$
declare
  stored public.email_verification_codes%rowtype;
begin
  select * into stored
  from public.email_verification_codes
  where user_id = target_user_id
  for update;

  if not found then
    return 'missing';
  end if;
  if stored.sent_at > now() - interval '1 minute' then
    return 'wait';
  end if;
  if stored.window_started_at > now() - interval '1 hour' and stored.send_count >= 5 then
    return 'limit';
  end if;

  update public.email_verification_codes
  set
    code_hash = submitted_code_hash,
    expires_at = new_expires_at,
    attempts = 0,
    sent_at = now(),
    window_started_at = case
      when stored.window_started_at <= now() - interval '1 hour' then now()
      else stored.window_started_at
    end,
    send_count = case
      when stored.window_started_at <= now() - interval '1 hour' then 1
      else stored.send_count + 1
    end
  where user_id = target_user_id;
  return 'sent';
end;
$$;

revoke all on function public.verify_signup_email_code(uuid, text) from public, anon, authenticated;
grant execute on function public.verify_signup_email_code(uuid, text) to service_role;
revoke all on function public.issue_signup_email_code(uuid, text, timestamptz) from public, anon, authenticated;
grant execute on function public.issue_signup_email_code(uuid, text, timestamptz) to service_role;
