/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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.
 */

#pragma once

#include <map>
#include <memory>
#include <string>
#include <vector>

#include "paimon/catalog/catalog.h"
#include "paimon/catalog/table.h"
#include "paimon/core/schema/schema_manager.h"
#include "paimon/logging.h"
#include "paimon/result.h"
#include "paimon/status.h"

struct ArrowSchema;

namespace paimon {
class TableSchema;
class FileSystem;
class Identifier;
class Logger;

class FileSystemCatalog : public Catalog {
 public:
    FileSystemCatalog(const std::shared_ptr<FileSystem>& fs, const std::string& warehouse,
                      const std::map<std::string, std::string>& catalog_options);

    Status CreateDatabase(const std::string& db_name,
                          const std::map<std::string, std::string>& options,
                          bool ignore_if_exists) override;
    Status CreateTable(const Identifier& identifier, ArrowSchema* c_schema,
                       const std::vector<std::string>& partition_keys,
                       const std::vector<std::string>& primary_keys,
                       const std::map<std::string, std::string>& options,
                       bool ignore_if_exists) override;
    Status DropDatabase(const std::string& name, bool ignore_if_not_exists, bool cascade) override;
    Status DropTable(const Identifier& identifier, bool ignore_if_not_exists) override;
    Status RenameTable(const Identifier& from_table, const Identifier& to_table,
                       bool ignore_if_not_exists) override;
    Result<std::vector<std::string>> ListDatabases() const override;
    Result<std::vector<std::string>> ListTables(const std::string& db_name) const override;
    Result<bool> DatabaseExists(const std::string& db_name) const override;
    Result<bool> TableExists(const Identifier& identifier) const override;
    std::string GetDatabaseLocation(const std::string& db_name) const override;
    Result<std::string> GetTableLocation(const Identifier& identifier) const override;
    Result<std::shared_ptr<Schema>> LoadTableSchema(const Identifier& identifier) const override;
    std::string GetRootPath() const override;
    std::shared_ptr<FileSystem> GetFileSystem() const override;
    const std::map<std::string, std::string>& GetOptions() const override;
    Result<std::shared_ptr<Table>> GetTable(const Identifier& identifier) const override;
    Result<std::vector<SnapshotInfo>> ListSnapshots(const Identifier& identifier,
                                                    const std::string& branch) const override;

 private:
    static std::string NewDatabasePath(const std::string& warehouse, const std::string& db_name);
    static Result<std::string> NewDataTablePath(const std::string& warehouse,
                                                const Identifier& identifier);
    static bool IsSystemDatabase(const std::string& db_name);
    static Result<bool> IsSpecifiedSystemTable(const Identifier& identifier);
    static Result<bool> IsSystemTable(const Identifier& identifier);
    Result<std::optional<std::shared_ptr<TableSchema>>> TableSchemaExists(
        const Identifier& identifier) const;

    Status CreateDatabaseImpl(const std::string& db_name,
                              const std::map<std::string, std::string>& options);
    Result<bool> TableExistsInFileSystem(const std::string& table_path) const;

    // Get all external paths from a list of schemas
    Result<std::vector<std::string>> GetSchemaExternalPaths(
        const std::vector<std::shared_ptr<TableSchema>>& schemas) const;

    // Get all branch names for a table
    Result<std::vector<std::string>> GetTableBranches(const std::string& table_path) const;

    // Drop table implementation with external paths cleanup
    Status DropTableImpl(const Identifier& identifier,
                         const std::vector<std::string>& external_paths);

    std::shared_ptr<FileSystem> fs_;
    std::string warehouse_;
    std::map<std::string, std::string> catalog_options_;

    std::shared_ptr<Logger> logger_;
};

}  // namespace paimon
