-- Current organization directory, UTC. Additive; existing rightsizing data preserved.
CREATE TABLE IF NOT EXISTS finops_gcp_project_bronze (
 organization_id VARCHAR(32) CHARACTER SET ascii NOT NULL,
 snapshot_day DATE NOT NULL,
 resource_kind VARCHAR(16) CHARACTER SET ascii NOT NULL,
 resource_key VARCHAR(320) CHARACTER SET ascii NOT NULL,
 payload LONGTEXT NOT NULL CHECK (JSON_VALID(payload)),
 observed_at DATETIME(6) NOT NULL,
 ingestion_run_id CHAR(36) CHARACTER SET ascii NOT NULL,
 PRIMARY KEY(organization_id,snapshot_day,resource_kind,resource_key), KEY ix_day(snapshot_day)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS finops_gcp_folder_silver (
 organization_id VARCHAR(32) CHARACTER SET ascii NOT NULL,
 folder_id VARCHAR(32) CHARACTER SET ascii NOT NULL,
 parent_resource VARCHAR(128) CHARACTER SET ascii NOT NULL,
 display_name VARCHAR(256) NOT NULL,
 lifecycle_state VARCHAR(32) CHARACTER SET ascii NOT NULL,
 is_present BOOLEAN NOT NULL,
 first_seen_at DATETIME(6) NOT NULL,
 last_seen_at DATETIME(6) NOT NULL,
 ingestion_run_id CHAR(36) CHARACTER SET ascii NOT NULL,
 PRIMARY KEY(organization_id,folder_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS finops_gcp_project_silver (
 organization_id VARCHAR(32) CHARACTER SET ascii NOT NULL,
 project_number VARCHAR(32) CHARACTER SET ascii NOT NULL,
 project_id VARCHAR(128) CHARACTER SET ascii NOT NULL,
 parent_resource VARCHAR(128) CHARACTER SET ascii NOT NULL,
 display_name VARCHAR(256) NOT NULL,
 lifecycle_state VARCHAR(32) CHARACTER SET ascii NOT NULL,
 labels_json LONGTEXT NOT NULL CHECK (JSON_VALID(labels_json)),
 is_present BOOLEAN NOT NULL,
 is_selected BOOLEAN NOT NULL,
 first_seen_at DATETIME(6) NOT NULL,
 last_seen_at DATETIME(6) NOT NULL,
 ingestion_run_id CHAR(36) CHARACTER SET ascii NOT NULL,
 PRIMARY KEY(organization_id,project_number),
 UNIQUE KEY uk_project(organization_id,project_id), KEY ix_scope(organization_id,is_present,is_selected,lifecycle_state)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS finops_gcp_project_service_silver (
 organization_id VARCHAR(32) CHARACTER SET ascii NOT NULL,
 project_number VARCHAR(32) CHARACTER SET ascii NOT NULL,
 service_name VARCHAR(255) CHARACTER SET ascii NOT NULL,
 service_state VARCHAR(32) CHARACTER SET ascii NOT NULL,
 first_seen_at DATETIME(6) NOT NULL,
 last_seen_at DATETIME(6) NOT NULL,
 ingestion_run_id CHAR(36) CHARACTER SET ascii NOT NULL,
 PRIMARY KEY(organization_id,project_number,service_name),
 KEY ix_service(organization_id,service_name,service_state),
 FOREIGN KEY(organization_id,project_number) REFERENCES finops_gcp_project_silver(organization_id,project_number)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS finops_gcp_project_discovery_status (
 organization_id VARCHAR(32) CHARACTER SET ascii NOT NULL,
 scope_hash CHAR(64) CHARACTER SET ascii NOT NULL,
 collected_at DATETIME(6) NOT NULL,
 ingestion_run_id CHAR(36) CHARACTER SET ascii NOT NULL,
 PRIMARY KEY(organization_id)
) ENGINE=InnoDB;
