SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
GO

IF OBJECT_ID(N'dbo.StoryIntelligenceCharacterIllustrationAssignments', N'U') IS NULL
BEGIN
    CREATE TABLE dbo.StoryIntelligenceCharacterIllustrationAssignments
    (
        StoryIntelligenceCharacterIllustrationAssignmentID int IDENTITY(1,1) NOT NULL CONSTRAINT PK_StoryIntelligenceCharacterIllustrationAssignments PRIMARY KEY,
        ProjectID int NOT NULL,
        CharacterKey nvarchar(160) NOT NULL,
        CharacterName nvarchar(200) NOT NULL,
        IllustrationLibraryItemID int NULL,
        StableCode nvarchar(120) NULL,
        MatchConfidence decimal(5,2) NOT NULL CONSTRAINT DF_SICharacterIllustrationAssignments_MatchConfidence DEFAULT 0,
        MatchingScore int NOT NULL CONSTRAINT DF_SICharacterIllustrationAssignments_MatchingScore DEFAULT 0,
        EvidenceJson nvarchar(max) NULL,
        CandidateDiagnosticsJson nvarchar(max) NULL,
        ReasonChosen nvarchar(500) NULL,
        ReasonReassigned nvarchar(500) NULL,
        IsActive bit NOT NULL CONSTRAINT DF_SICharacterIllustrationAssignments_IsActive DEFAULT 1,
        CreatedUtc datetime2(0) NOT NULL CONSTRAINT DF_SICharacterIllustrationAssignments_CreatedUtc DEFAULT SYSUTCDATETIME(),
        UpdatedUtc datetime2(0) NOT NULL CONSTRAINT DF_SICharacterIllustrationAssignments_UpdatedUtc DEFAULT SYSUTCDATETIME()
    );
END;
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = N'UX_SICharacterIllustrationAssignments_ProjectCharacter' AND object_id = OBJECT_ID(N'dbo.StoryIntelligenceCharacterIllustrationAssignments'))
BEGIN
    CREATE UNIQUE INDEX UX_SICharacterIllustrationAssignments_ProjectCharacter
    ON dbo.StoryIntelligenceCharacterIllustrationAssignments(ProjectID, CharacterKey)
    WHERE IsActive = 1;
END;
GO

IF NOT EXISTS (SELECT 1 FROM sys.foreign_keys WHERE name = N'FK_SICharacterIllustrationAssignments_Project')
BEGIN
    ALTER TABLE dbo.StoryIntelligenceCharacterIllustrationAssignments
        ADD CONSTRAINT FK_SICharacterIllustrationAssignments_Project
        FOREIGN KEY (ProjectID) REFERENCES dbo.Projects(ProjectID);
END;
GO

IF NOT EXISTS (SELECT 1 FROM sys.foreign_keys WHERE name = N'FK_SICharacterIllustrationAssignments_Illustration')
BEGIN
    ALTER TABLE dbo.StoryIntelligenceCharacterIllustrationAssignments
        ADD CONSTRAINT FK_SICharacterIllustrationAssignments_Illustration
        FOREIGN KEY (IllustrationLibraryItemID) REFERENCES dbo.IllustrationLibraryItems(IllustrationLibraryItemID);
END;
GO

IF OBJECT_ID(N'dbo.StoryIntelligenceIllustrationDemands', N'U') IS NULL
BEGIN
    CREATE TABLE dbo.StoryIntelligenceIllustrationDemands
    (
        StoryIntelligenceIllustrationDemandID int IDENTITY(1,1) NOT NULL CONSTRAINT PK_StoryIntelligenceIllustrationDemands PRIMARY KEY,
        ProjectID int NULL,
        DemandKey nvarchar(240) NOT NULL,
        AgeBand nvarchar(40) NOT NULL,
        Presentation nvarchar(40) NOT NULL,
        HairColour nvarchar(40) NOT NULL,
        SkinTone nvarchar(40) NOT NULL,
        HairLength nvarchar(40) NULL,
        Glasses nvarchar(20) NULL,
        FacialHair nvarchar(40) NULL,
        RequestCount int NOT NULL CONSTRAINT DF_StoryIntelligenceIllustrationDemands_RequestCount DEFAULT 1,
        LastRequestedUtc datetime2(0) NOT NULL CONSTRAINT DF_StoryIntelligenceIllustrationDemands_LastRequestedUtc DEFAULT SYSUTCDATETIME(),
        CreatedUtc datetime2(0) NOT NULL CONSTRAINT DF_StoryIntelligenceIllustrationDemands_CreatedUtc DEFAULT SYSUTCDATETIME(),
        UpdatedUtc datetime2(0) NOT NULL CONSTRAINT DF_StoryIntelligenceIllustrationDemands_UpdatedUtc DEFAULT SYSUTCDATETIME()
    );
END;
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = N'UX_StoryIntelligenceIllustrationDemands_ProjectDemand' AND object_id = OBJECT_ID(N'dbo.StoryIntelligenceIllustrationDemands'))
BEGIN
    CREATE UNIQUE INDEX UX_StoryIntelligenceIllustrationDemands_ProjectDemand
    ON dbo.StoryIntelligenceIllustrationDemands(ProjectID, DemandKey);
END;
GO
