DROP TABLE IF EXISTS imdb_ratings CASCADE;
CREATE TABLE imdb_ratings (
    imdb_id text PRIMARY KEY NOT NULL,
    rating real NOT NULL DEFAULT 0,
    votes int NOT NULL DEFAULT 1,
    LIKE base INCLUDING DEFAULTS
);

CREATE INDEX ON imdb_ratings (imdb_id);
CREATE TRIGGER update_imdb_ratings_updated_at BEFORE UPDATE ON imdb_ratings FOR EACH ROW EXECUTE PROCEDURE  update_updated_at_column();

CREATE OR REPLACE VIEW movies_with_rating AS SELECT m.id, m.imdb_id, m.title, m.plot, m.tmdb_id, m.year, m.original_title, m.runtime, m.sort_title, m.tagline, m.genres, r.rating, r.votes, m.updated_at, m.created_at FROM movies m JOIN imdb_ratings r ON m.imdb_id = r.imdb_id;
CREATE OR REPLACE VIEW shows_with_rating AS SELECT s.id, s.imdb_id, s.title, s.plot, s.tvdb_id, s.year, s.first_aired, r.rating, r.votes, s.updated_at, s.created_at FROM shows s JOIN imdb_ratings r ON s.imdb_id = r.imdb_id;

