CREATE TABLE IF NOT EXISTS Movies (
    MovieID INTEGER PRIMARY KEY AUTOINCREMENT,
    MovieURI TEXT UNIQUE NOT NULL
);


CREATE TABLE IF NOT EXISTS WikiPageIDs (
    MovieID INTEGER PRIMARY KEY,
    PageID INTEGER UNIQUE NOT NULL,
    FOREIGN KEY(MovieID) REFERENCES Movies(MovieID)
);


CREATE TABLE IF NOT EXISTS WikipediaAbstracts (
    MovieID INTEGER PRIMARY KEY,
    Abstract TEXT NOT NULL,
    FOREIGN KEY(MovieID) REFERENCES Movies(MovieID)
);


CREATE TABLE IF NOT EXISTS Origins (
    OriginID INTEGER PRIMARY KEY AUTOINCREMENT,
    OriginName TEXT UNIQUE NOT NULL
);


CREATE TABLE IF NOT EXISTS Subjects (
    SubjectID INTEGER PRIMARY KEY AUTOINCREMENT,
    SubjectURI TEXT UNIQUE NOT NULL,
    OriginID BIGINT NOT NULL,
    FOREIGN KEY(OriginID) REFERENCES Origins(OriginID)
);


CREATE TABLE IF NOT EXISTS Relationships (
    RelationshipID INTEGER PRIMARY KEY AUTOINCREMENT,
    RelationshipURI TEXT UNIQUE NOT NULL
);


CREATE TABLE IF NOT EXISTS Objects (
    ObjectID INTEGER PRIMARY KEY AUTOINCREMENT,
    ObjectURI TEXT UNIQUE NOT NULL,
    OriginID BIGINT NOT NULL,
    FOREIGN KEY(OriginID) REFERENCES Origins(OriginID)
);

CREATE TABLE IF NOT EXISTS RDFs (
    RDF_ID INTEGER PRIMARY KEY AUTOINCREMENT,
    MovieID INTEGER NOT NULL,
    SubjectID INTEGER NOT NULL,
    RelationshipID INTEGER NOT NULL,
    ObjectID INTEGER NOT NULL,
    UNIQUE(SubjectID, RelationshipID, ObjectID),
    FOREIGN KEY(MovieID) REFERENCES Movies(MovieID),
    FOREIGN KEY(SubjectID) REFERENCES Subjects(SubjectID),
    FOREIGN KEY(RelationshipID) REFERENCES Relationships(RelationshipID),
    FOREIGN KEY(ObjectID) REFERENCES Objects(ObjectID)
);

CREATE INDEX IF NOT EXISTS idx_rdf_movie_id ON RDFs(MovieID);
CREATE INDEX IF NOT EXISTS idx_rdf_subject_id ON RDFs(SubjectID);
CREATE INDEX IF NOT EXISTS idx_rdf_relationship_id ON RDFs(RelationshipID);
CREATE INDEX IF NOT EXISTS idx_rdf_object_id ON RDFs(ObjectID);

CREATE TABLE IF NOT EXISTS Abbreviations (
    AbbreviationID INTEGER PRIMARY KEY AUTOINCREMENT,
    URI TEXT UNIQUE NOT NULL,
    Abbreviation TEXT UNIQUE NOT NULL
);

CREATE TABLE IF NOT EXISTS Subjects_Abbreviations (
    SubjectID INTEGER NOT NULL,
    AbbreviationID INTEGER NOT NULL,
    PRIMARY KEY(SubjectID, AbbreviationID),
    FOREIGN KEY(SubjectID) REFERENCES Subjects(SubjectID),
    FOREIGN KEY(AbbreviationID) REFERENCES Abbreviations(AbbreviationID)
);

CREATE TABLE IF NOT EXISTS Relationships_Abbreviations (
    RelationshipID INTEGER NOT NULL,
    AbbreviationID INTEGER NOT NULL,
    PRIMARY KEY(RelationshipID, AbbreviationID),
    FOREIGN KEY(RelationshipID) REFERENCES Relationships(RelationshipID),
    FOREIGN KEY(AbbreviationID) REFERENCES Abbreviations(AbbreviationID)
);

CREATE TABLE IF NOT EXISTS Objects_Abbreviations (
    ObjectID INTEGER NOT NULL,
    AbbreviationID INTEGER NOT NULL,
    PRIMARY KEY(ObjectID, AbbreviationID),
    FOREIGN KEY(ObjectID) REFERENCES Objects(ObjectID),
    FOREIGN KEY(AbbreviationID) REFERENCES Abbreviations(AbbreviationID)
);

CREATE INDEX IF NOT EXISTS idx_sub_abbr_sub_id ON Subjects_Abbreviations(SubjectID);
CREATE INDEX IF NOT EXISTS idx_sub_abbr_abbr_id ON Subjects_Abbreviations(AbbreviationID);
CREATE INDEX IF NOT EXISTS idx_rel_abbr_rel_id ON Relationships_Abbreviations(RelationshipID);
CREATE INDEX IF NOT EXISTS idx_rel_abbr_abbr_id ON Relationships_Abbreviations(AbbreviationID);
CREATE INDEX IF NOT EXISTS idx_obj_abbr_obj_id ON Objects_Abbreviations(ObjectID);
CREATE INDEX IF NOT EXISTS idx_obj_abbr_abbr_id ON Objects_Abbreviations(AbbreviationID);

-- Views
-- Subjects
CREATE VIEW IF NOT EXISTS ParsedSubjects
AS
SELECT
	SubjectID,
	CASE WHEN Abbreviation IS NULL
		THEN SubjectURI
		ELSE Abbreviation || ':' || replace(SubjectURI, URI, '') END
		AS SubjectURI
FROM Subjects
	LEFT JOIN Subjects_Abbreviations USING (SubjectID)
	LEFT JOIN Abbreviations USING (AbbreviationID);

-- Relationships
CREATE VIEW IF NOT EXISTS ParsedRelationships
AS
SELECT
	RelationshipID,
	CASE WHEN Abbreviation IS NULL
		THEN RelationshipURI
		ELSE Abbreviation || ':' || replace(RelationshipURI, URI, '') END
		AS RelationshipURI
FROM Relationships
	LEFT JOIN Relationships_Abbreviations USING (RelationshipID)
	LEFT JOIN Abbreviations USING (AbbreviationID)

-- Objects
CREATE VIEW IF NOT EXISTS ParsedObjects
AS
SELECT
	ObjectID,
	CASE WHEN Abbreviation IS NULL
		THEN ObjectURI
		ELSE Abbreviation || ':' || replace(ObjectURI, URI, '') END
		AS ObjectURI
FROM Objects
	LEFT JOIN Objects_Abbreviations USING (ObjectID)
	LEFT JOIN Abbreviations USING (AbbreviationID);







