SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO

IF COL_LENGTH(N'dbo.Locations', N'ExcludeFromCompanionDetection') IS NULL
    ALTER TABLE dbo.Locations ADD ExcludeFromCompanionDetection bit NOT NULL CONSTRAINT DF_Locations_ExcludeFromCompanionDetection DEFAULT (0);
GO

IF COL_LENGTH(N'dbo.Locations', N'DetectionPriority') IS NULL
    ALTER TABLE dbo.Locations ADD DetectionPriority int NOT NULL CONSTRAINT DF_Locations_DetectionPriority DEFAULT (50);
GO

CREATE OR ALTER VIEW dbo.LocationPaths
AS
WITH LocationTree AS
(
    SELECT LocationID, ProjectID, ParentLocationID, LocationName, LocationTypeID, Description, ShowInQuickAddBar,
        ExcludeFromCompanionDetection, DetectionPriority,
        CAST(LocationName AS nvarchar(max)) AS LocationPath,
        CAST(RIGHT('000000' + CAST(LocationID AS varchar(6)), 6) AS nvarchar(max)) AS SortPath,
        0 AS Depth
    FROM dbo.Locations
    WHERE ParentLocationID IS NULL AND IsArchived = 0
    UNION ALL
    SELECT child.LocationID, child.ProjectID, child.ParentLocationID, child.LocationName, child.LocationTypeID, child.Description, child.ShowInQuickAddBar,
        child.ExcludeFromCompanionDetection, child.DetectionPriority,
        CAST(parent.LocationPath + N' > ' + child.LocationName AS nvarchar(max)),
        CAST(parent.SortPath + N'.' + RIGHT('000000' + CAST(child.LocationID AS varchar(6)), 6) AS nvarchar(max)),
        parent.Depth + 1
    FROM dbo.Locations child
    INNER JOIN LocationTree parent ON parent.LocationID = child.ParentLocationID
    WHERE child.IsArchived = 0
)
SELECT LocationID, ProjectID, ParentLocationID, LocationName, LocationTypeID, Description, ShowInQuickAddBar,
    ExcludeFromCompanionDetection, DetectionPriority, LocationPath, SortPath, Depth
FROM LocationTree;
GO

CREATE OR ALTER PROCEDURE dbo.Location_ListByProject
    @ProjectID int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT lp.LocationID, lp.ProjectID, lp.ParentLocationID, lp.LocationName, lp.LocationTypeID, lt.TypeName AS LocationTypeName,
        lp.Description, lp.ShowInQuickAddBar, lp.ExcludeFromCompanionDetection, lp.DetectionPriority,
        lp.LocationPath, lp.Depth, l.CreatedDate, l.UpdatedDate, l.IsArchived
    FROM dbo.LocationPaths lp
    INNER JOIN dbo.Locations l ON l.LocationID = lp.LocationID
    LEFT JOIN dbo.LocationTypes lt ON lt.LocationTypeID = lp.LocationTypeID
    WHERE lp.ProjectID = @ProjectID
    ORDER BY lp.SortPath;
END;
GO

CREATE OR ALTER PROCEDURE dbo.Location_Get
    @LocationID int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT lp.LocationID, lp.ProjectID, lp.ParentLocationID, parent.LocationName AS ParentLocationName,
        lp.LocationName, lp.LocationTypeID, lt.TypeName AS LocationTypeName, lp.Description, lp.ShowInQuickAddBar,
        lp.ExcludeFromCompanionDetection, lp.DetectionPriority, lp.LocationPath, lp.Depth,
        l.CreatedDate, l.UpdatedDate, l.IsArchived
    FROM dbo.LocationPaths lp
    INNER JOIN dbo.Locations l ON l.LocationID = lp.LocationID
    LEFT JOIN dbo.Locations parent ON parent.LocationID = lp.ParentLocationID
    LEFT JOIN dbo.LocationTypes lt ON lt.LocationTypeID = lp.LocationTypeID
    WHERE lp.LocationID = @LocationID;
END;
GO

CREATE OR ALTER PROCEDURE dbo.Location_Save
    @LocationID int = NULL,
    @ProjectID int,
    @ParentLocationID int = NULL,
    @LocationName nvarchar(200),
    @LocationTypeID int = NULL,
    @Description nvarchar(max) = NULL,
    @ShowInQuickAddBar bit = 0,
    @ExcludeFromCompanionDetection bit = 0,
    @DetectionPriority int = 50
AS
BEGIN
    SET NOCOUNT ON;

    SET @DetectionPriority = COALESCE(@DetectionPriority, 50);

    IF @LocationID IS NULL OR @LocationID = 0
    BEGIN
        INSERT dbo.Locations (ProjectID, ParentLocationID, LocationName, LocationTypeID, Description, ShowInQuickAddBar, ExcludeFromCompanionDetection, DetectionPriority)
        VALUES (@ProjectID, @ParentLocationID, @LocationName, @LocationTypeID, @Description, @ShowInQuickAddBar, @ExcludeFromCompanionDetection, @DetectionPriority);

        SET @LocationID = CAST(SCOPE_IDENTITY() AS int);
    END
    ELSE
    BEGIN
        UPDATE dbo.Locations
        SET ParentLocationID = @ParentLocationID,
            LocationName = @LocationName,
            LocationTypeID = @LocationTypeID,
            Description = @Description,
            ShowInQuickAddBar = @ShowInQuickAddBar,
            ExcludeFromCompanionDetection = @ExcludeFromCompanionDetection,
            DetectionPriority = @DetectionPriority,
            UpdatedDate = SYSUTCDATETIME()
        WHERE LocationID = @LocationID;
    END

    SELECT @LocationID;
END;
GO

CREATE OR ALTER PROCEDURE dbo.WordCompanion_Runtime_LocationAlias_List
    @UserID int,
    @ProjectID int,
    @DocumentGuid uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;

    IF @DocumentGuid IS NULL
        OR @DocumentGuid = '00000000-0000-0000-0000-000000000000'
        OR @ProjectID <= 0
        RETURN;

    IF NOT EXISTS
    (
        SELECT 1
        FROM dbo.ManuscriptDocuments md
        INNER JOIN dbo.ProjectUserAccess pua ON pua.ProjectID = md.ProjectID
        INNER JOIN dbo.Projects p ON p.ProjectID = md.ProjectID
        WHERE md.DocumentGuid = @DocumentGuid
            AND md.ProjectID = @ProjectID
            AND md.IsActive = 1
            AND p.IsArchived = 0
            AND pua.UserID = @UserID
            AND pua.IsActive = 1
    )
        RETURN;

    SELECT lp.LocationID AS LocationId,
        lp.ParentLocationID AS ParentLocationId,
        lp.LocationName AS Name,
        lp.LocationPath,
        lp.LocationName AS MatchText,
        CAST(1 AS bit) AS IsDisplayName,
        lp.ExcludeFromCompanionDetection,
        lp.DetectionPriority
    FROM dbo.LocationPaths lp
    WHERE lp.ProjectID = @ProjectID
        AND NULLIF(LTRIM(RTRIM(lp.LocationName)), N'') IS NOT NULL

    UNION ALL

    SELECT lp.LocationID AS LocationId,
        lp.ParentLocationID AS ParentLocationId,
        lp.LocationName AS Name,
        lp.LocationPath,
        la.Alias AS MatchText,
        CAST(0 AS bit) AS IsDisplayName,
        lp.ExcludeFromCompanionDetection,
        lp.DetectionPriority
    FROM dbo.LocationPaths lp
    INNER JOIN dbo.LocationAliases la ON la.LocationID = lp.LocationID
    WHERE lp.ProjectID = @ProjectID
        AND NULLIF(LTRIM(RTRIM(la.Alias)), N'') IS NOT NULL
    ORDER BY DetectionPriority DESC, Name, LocationId, IsDisplayName DESC, MatchText;
END;
GO
