SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO

CREATE OR ALTER PROCEDURE dbo.FeatureRequest_GetForUser
    @FeatureRequestID int,
    @UserID int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT fr.FeatureRequestID, fr.UserID, au.Email AS UserEmail, au.DisplayName AS UserDisplayName,
        fr.Title, fr.Description, fr.AppArea, fr.Importance, fr.Status, fr.IsPublicCandidate,
        fr.AdminNotes, fr.CreatedUtc, fr.UpdatedUtc, fr.ClosedUtc, fr.ShippedUtc,
        COALESCE(latest.CreatedUtc, fr.UpdatedUtc, fr.CreatedUtc) AS LatestActivityUtc,
        latest.IsAdmin AS LatestMessageIsAdmin,
        latest.MessageBody AS LatestMessagePreview,
        messageCounts.MessageCount
    FROM dbo.FeatureRequests fr
    INNER JOIN dbo.AppUser au ON au.UserID = fr.UserID
    OUTER APPLY
    (
        SELECT TOP (1) frm.IsAdmin, frm.MessageBody, frm.CreatedUtc
        FROM dbo.FeatureRequestMessages frm
        WHERE frm.FeatureRequestID = fr.FeatureRequestID
        ORDER BY frm.CreatedUtc DESC, frm.FeatureRequestMessageID DESC
    ) latest
    OUTER APPLY
    (
        SELECT COUNT(1) AS MessageCount
        FROM dbo.FeatureRequestMessages frm
        WHERE frm.FeatureRequestID = fr.FeatureRequestID
    ) messageCounts
    WHERE fr.FeatureRequestID = @FeatureRequestID
        AND fr.UserID = @UserID;
END;
GO

CREATE OR ALTER PROCEDURE dbo.FeatureRequest_GetForAdmin
    @FeatureRequestID int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT fr.FeatureRequestID, fr.UserID, au.Email AS UserEmail, au.DisplayName AS UserDisplayName,
        fr.Title, fr.Description, fr.AppArea, fr.Importance, fr.Status, fr.IsPublicCandidate,
        fr.AdminNotes, fr.CreatedUtc, fr.UpdatedUtc, fr.ClosedUtc, fr.ShippedUtc,
        COALESCE(latest.CreatedUtc, fr.UpdatedUtc, fr.CreatedUtc) AS LatestActivityUtc,
        latest.IsAdmin AS LatestMessageIsAdmin,
        latest.MessageBody AS LatestMessagePreview,
        messageCounts.MessageCount
    FROM dbo.FeatureRequests fr
    INNER JOIN dbo.AppUser au ON au.UserID = fr.UserID
    OUTER APPLY
    (
        SELECT TOP (1) frm.IsAdmin, frm.MessageBody, frm.CreatedUtc
        FROM dbo.FeatureRequestMessages frm
        WHERE frm.FeatureRequestID = fr.FeatureRequestID
        ORDER BY frm.CreatedUtc DESC, frm.FeatureRequestMessageID DESC
    ) latest
    OUTER APPLY
    (
        SELECT COUNT(1) AS MessageCount
        FROM dbo.FeatureRequestMessages frm
        WHERE frm.FeatureRequestID = fr.FeatureRequestID
    ) messageCounts
    WHERE fr.FeatureRequestID = @FeatureRequestID;
END;
GO

CREATE OR ALTER PROCEDURE dbo.FeatureRequest_ListForUser
    @UserID int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT fr.FeatureRequestID, fr.UserID, au.Email AS UserEmail, au.DisplayName AS UserDisplayName,
        fr.Title, fr.Description, fr.AppArea, fr.Importance, fr.Status, fr.IsPublicCandidate,
        CAST(NULL AS nvarchar(max)) AS AdminNotes, fr.CreatedUtc, fr.UpdatedUtc, fr.ClosedUtc, fr.ShippedUtc,
        COALESCE(latest.CreatedUtc, fr.UpdatedUtc, fr.CreatedUtc) AS LatestActivityUtc,
        latest.IsAdmin AS LatestMessageIsAdmin,
        latest.MessageBody AS LatestMessagePreview,
        messageCounts.MessageCount
    FROM dbo.FeatureRequests fr
    INNER JOIN dbo.AppUser au ON au.UserID = fr.UserID
    OUTER APPLY
    (
        SELECT TOP (1) frm.IsAdmin, frm.MessageBody, frm.CreatedUtc
        FROM dbo.FeatureRequestMessages frm
        WHERE frm.FeatureRequestID = fr.FeatureRequestID
        ORDER BY frm.CreatedUtc DESC, frm.FeatureRequestMessageID DESC
    ) latest
    OUTER APPLY
    (
        SELECT COUNT(1) AS MessageCount
        FROM dbo.FeatureRequestMessages frm
        WHERE frm.FeatureRequestID = fr.FeatureRequestID
    ) messageCounts
    WHERE fr.UserID = @UserID
    ORDER BY COALESCE(latest.CreatedUtc, fr.UpdatedUtc, fr.CreatedUtc) DESC, fr.FeatureRequestID DESC;
END;
GO

CREATE OR ALTER PROCEDURE dbo.FeatureRequest_ListForAdmin
    @Status nvarchar(50) = NULL,
    @AppArea nvarchar(100) = NULL,
    @Importance nvarchar(50) = NULL,
    @Search nvarchar(200) = NULL,
    @Sort nvarchar(50) = N'LatestActivity'
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @CleanSearch nvarchar(200) = NULLIF(LTRIM(RTRIM(@Search)), N'');

    SELECT fr.FeatureRequestID, fr.UserID, au.Email AS UserEmail, au.DisplayName AS UserDisplayName,
        fr.Title, fr.Description, fr.AppArea, fr.Importance, fr.Status, fr.IsPublicCandidate,
        fr.AdminNotes, fr.CreatedUtc, fr.UpdatedUtc, fr.ClosedUtc, fr.ShippedUtc,
        COALESCE(latest.CreatedUtc, fr.UpdatedUtc, fr.CreatedUtc) AS LatestActivityUtc,
        latest.IsAdmin AS LatestMessageIsAdmin,
        latest.MessageBody AS LatestMessagePreview,
        messageCounts.MessageCount
    FROM dbo.FeatureRequests fr
    INNER JOIN dbo.AppUser au ON au.UserID = fr.UserID
    OUTER APPLY
    (
        SELECT TOP (1) frm.IsAdmin, frm.MessageBody, frm.CreatedUtc
        FROM dbo.FeatureRequestMessages frm
        WHERE frm.FeatureRequestID = fr.FeatureRequestID
        ORDER BY frm.CreatedUtc DESC, frm.FeatureRequestMessageID DESC
    ) latest
    OUTER APPLY
    (
        SELECT COUNT(1) AS MessageCount
        FROM dbo.FeatureRequestMessages frm
        WHERE frm.FeatureRequestID = fr.FeatureRequestID
    ) messageCounts
    WHERE (@Status IS NULL OR fr.Status = @Status)
        AND (@AppArea IS NULL OR fr.AppArea = @AppArea)
        AND (@Importance IS NULL OR fr.Importance = @Importance)
        AND
        (
            @CleanSearch IS NULL
            OR fr.Title LIKE N'%' + @CleanSearch + N'%'
            OR fr.Description LIKE N'%' + @CleanSearch + N'%'
            OR au.DisplayName LIKE N'%' + @CleanSearch + N'%'
            OR au.Email LIKE N'%' + @CleanSearch + N'%'
        )
    ORDER BY
        CASE WHEN @Sort = N'OldestFirst' THEN fr.CreatedUtc END ASC,
        CASE WHEN @Sort = N'NewestFirst' THEN fr.CreatedUtc END DESC,
        CASE WHEN @Sort IS NULL OR @Sort = N'LatestActivity' THEN COALESCE(latest.CreatedUtc, fr.UpdatedUtc, fr.CreatedUtc) END DESC,
        fr.FeatureRequestID DESC;
END;
GO
