CREATE TABLE IF NOT EXISTS road_graph.covered_edge_v2 (
    persistent_edge_id text NOT NULL,
    camera_deviation smallint NOT NULL,
    privacy signals.feature_privacy NOT NULL,
    coverage_fraction real NOT NULL,
    actualization_date timestamp with time zone NOT NULL,
    CONSTRAINT camera_deviation_check
        CHECK (camera_deviation IN (0, 90, 180, 270)),
    CONSTRAINT coverage_fraction_check
        CHECK (.0 < coverage_fraction AND coverage_fraction <= 1.),
    PRIMARY KEY (persistent_edge_id, camera_deviation, privacy)
);

COMMENT ON COLUMN road_graph.covered_edge_v2.camera_deviation IS
    'degrees between heading and camera direction, measured clockwise';

CREATE TABLE IF NOT EXISTS road_graph.edge_covering_feature_v2 (
    persistent_edge_id text NOT NULL,
    camera_deviation smallint NOT NULL,
    privacy signals.feature_privacy NOT NULL,
    feature_id bigint NOT NULL
        REFERENCES signals.feature_transaction (feature_id),
    CONSTRAINT camera_deviation_check
        CHECK (camera_deviation IN (0, 90, 180, 270)),
    FOREIGN KEY (persistent_edge_id, camera_deviation, privacy)
        REFERENCES road_graph.covered_edge_v2 (persistent_edge_id, camera_deviation, privacy)
        ON DELETE CASCADE
);

COMMENT ON COLUMN road_graph.edge_covering_feature_v2.camera_deviation IS
    'degrees between heading and camera direction, measured clockwise';

CREATE INDEX IF NOT EXISTS edge_covering_feature_v2_feature_id_idx
    ON road_graph.edge_covering_feature_v2 (feature_id);

CREATE INDEX IF NOT EXISTS edge_covering_feature_v2_persistent_edge_id_camera_deviation_privacy_idx
    ON road_graph.edge_covering_feature_v2 (persistent_edge_id, camera_deviation, privacy);
