/*
 * Licensed to Crate.io GmbH ("Crate") under one or more contributor
 * license agreements.  See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.  Crate licenses
 * this file to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.  You may
 * obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *
 * However, if you have executed another commercial license agreement
 * with Crate these terms will supersede the license and you may use the
 * software solely pursuant to the terms of the relevant commercial agreement.
 */

package io.crate.replication.logical.metadata.pgcatalog;

import io.crate.execution.engine.collect.sources.InformationSchemaIterables;
import io.crate.metadata.RelationName;
import io.crate.metadata.Schemas;
import io.crate.metadata.SystemTable;
import io.crate.metadata.doc.DocTableInfo;
import io.crate.metadata.pgcatalog.PgCatalogSchemaInfo;
import io.crate.replication.logical.LogicalReplicationService;

import java.util.stream.Stream;

import static io.crate.types.DataTypes.STRING;

public class PgPublicationTablesTable {

    public static final RelationName IDENT = new RelationName(PgCatalogSchemaInfo.NAME, "pg_publication_tables");

    public static SystemTable<PublicatedTableRow> create() {
        return SystemTable.<PublicatedTableRow>builder(IDENT)
            .add("pubname", STRING, p -> p.publicationName)
            .add("schemaname", STRING, p -> p.relationName.schema())
            .add("tablename", STRING, p -> p.relationName.name())
            .build();
    }

    public static Iterable<PublicatedTableRow> rows(LogicalReplicationService logicalReplicationService,
                                                    Schemas schemas) {
        Stream<PublicatedTableRow> s = logicalReplicationService.publications().entrySet().stream()
            .mapMulti(
                (e, c) -> {
                    var pub = e.getValue();
                    var tables = pub.tables();
                    if (pub.isForAllTables()) {
                        // -> FOR ALL TABLES (expands to all current tables here)
                        InformationSchemaIterables.tablesStream(schemas)
                            .filter(t -> t instanceof DocTableInfo)
                            .forEach(t -> c.accept(new PublicatedTableRow(e.getKey(), t.ident(), pub.owner())));
                    } else {
                        tables.forEach(t -> c.accept(new PublicatedTableRow(e.getKey(), t, pub.owner())));
                    }
                }
            );
        return s::iterator;
    }

    public static class PublicatedTableRow {
        private final String publicationName;
        private final RelationName relationName;
        private final String owner;

        public PublicatedTableRow(String publicationName,
                                  RelationName relationName,
                                  String owner) {
            this.publicationName = publicationName;
            this.relationName = relationName;
            this.owner = owner;
        }

        public String owner() {
            return owner;
        }
    }
}

@@ -2173,24 +2173,51 @@
  schemas) %7B%0A
+        return () -%3E %7B%0A    
         Stre
@@ -2316,16 +2316,20 @@
         
+    
 .mapMult
@@ -2331,16 +2331,20 @@
 pMulti(%0A
+    
         
@@ -2359,24 +2359,28 @@
 (e, c) -%3E %7B%0A
+    
             
@@ -2427,24 +2427,28 @@
             
+    
 var tables =
@@ -2482,16 +2482,20 @@
         
+   
+ 
 if (pub.
@@ -2510,24 +2510,28 @@
 Tables()) %7B%0A
+    
             
@@ -2624,16 +2624,20 @@
         
+ 
+   
 Informat
@@ -2677,16 +2677,20 @@
 chemas)%0A
+    
         
@@ -2777,16 +2777,20 @@
         
+    
 .forEach
@@ -2886,16 +2886,20 @@
         
+  
+  
 %7D else %7B
@@ -2899,16 +2899,20 @@
  else %7B%0A
+    
         
@@ -3022,34 +3022,41 @@
                 
-%7D%0A
+    %7D%0A   
                 
@@ -3047,34 +3047,39 @@
                 
+ 
 %7D%0A
+    
             );%0A 
@@ -3089,16 +3089,20 @@
     
+  
+  
 return s
 ::it
@@ -3101,18 +3101,17 @@
 rn s
-::
+.
 iterator
 ;%0A  
@@ -3106,16 +3106,29 @@
 iterator
+();%0A        %7D
 ;%0A    %7D%0A

Fix iterator generation of pg_publication_tables rows