CREATE OR ALTER PROCEDURE sp_User_Register
    @Username NVARCHAR(50),
    @PasswordHash NVARCHAR(255),
    @Email NVARCHAR(100),
    @NombreCompleto NVARCHAR(100)
AS
BEGIN
    SET NOCOUNT ON;

    -- Check if username already exists
    IF EXISTS (SELECT 1 FROM Users WHERE Username = @Username)
    BEGIN
        THROW 50001, 'El nombre de usuario ya existe.', 1;
    END

    -- Check if email already exists
    IF EXISTS (SELECT 1 FROM Users WHERE Email = @Email)
    BEGIN
        THROW 50002, 'El email ya existe.', 1;
    END

    -- Insert new user
    INSERT INTO Users (Username, PasswordHash, Email, NombreCompleto)
    OUTPUT INSERTED.Id, INSERTED.Username, INSERTED.Email, INSERTED.NombreCompleto, INSERTED.FechaCreacion
    VALUES (@Username, @PasswordHash, @Email, @NombreCompleto);
END