CREATE TABLE IF NOT EXISTS Movies (
    MovieID BIGINT PRIMARY KEY AUTO INCREMENT,
    MovieURI TEXT UNIQUE NOT NULL
);


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


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


CREATE TABLE IF NOT EXISTS Origins (
    OriginID BIGINT PRIMARY KEY AUTO INCREMENT,
    OriginName TEXT UNIQUE NOT NULL
);


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


CREATE TABLE IF NOT EXISTS Relationships (
    RelationshipID BIGINT PRIMARY KEY AUTO INCREMENT,
    RelationshipURI TEXT UNIQUE NOT NULL
);


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

CREATE TABLE IF NOT EXISTS RDFs (
    RDF_ID BIGINT PRIMARY KEY AUTO INCREMENT,
    MovieID BIGINT NOT NULL,
    SubjectID BIGINT NOT NULL,
    RelationshipID BIGINT NOT NULL,
    ObjectID BIGINT 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);

