
-- Fix the admin_create_user function to use proper password hashing
CREATE OR REPLACE FUNCTION public.admin_create_user(
  user_email TEXT,
  user_full_name TEXT DEFAULT NULL,
  user_function_id UUID DEFAULT NULL,
  user_privilege_id UUID DEFAULT NULL
)
RETURNS UUID
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = public
AS $$
DECLARE
  new_user_id UUID;
  temp_password TEXT := '1234';
  hashed_password TEXT;
BEGIN
  -- Check if current user is admin
  IF NOT public.has_role(auth.uid(), 'admin'::app_role) THEN
    RAISE EXCEPTION 'Only admins can create new users';
  END IF;

  -- Check if email already exists
  IF EXISTS (SELECT 1 FROM auth.users WHERE email = user_email) THEN
    RAISE EXCEPTION 'User with this email already exists';
  END IF;

  -- Generate a simple hash for the password (Supabase will handle proper hashing)
  hashed_password := encode(digest(temp_password, 'sha256'), 'hex');

  -- Create user in auth.users (this will trigger the profile creation)
  INSERT INTO auth.users (
    instance_id,
    id,
    aud,
    role,
    email,
    encrypted_password,
    email_confirmed_at,
    created_at,
    updated_at,
    confirmation_token,
    email_change,
    email_change_token_new,
    recovery_token
  ) VALUES (
    '00000000-0000-0000-0000-000000000000',
    gen_random_uuid(),
    'authenticated',
    'authenticated',
    user_email,
    hashed_password,
    now(),
    now(),
    now(),
    '',
    '',
    '',
    ''
  ) RETURNING id INTO new_user_id;

  -- Update the profile with additional information
  UPDATE public.profiles 
  SET 
    full_name = user_full_name,
    function_id = user_function_id,
    privilege_id = user_privilege_id,
    status = 'active'
  WHERE id = new_user_id;

  RETURN new_user_id;
END;
$$;
