use crate::schema::{IndexInfo, PartitionDefinition, TableInfo};

pub fn create_partition_table_and_index_from_def(
    table_info: &TableInfo,
    index_info: &IndexInfo,
    def: PartitionDefinition,
) -> (TableInfo, IndexInfo) {
    let partition_table_info = TableInfo {
        keyspace_id: table_info.keyspace_id,
        table_id: def.table_id,
        table_name: table_info.table_name.clone(),
        database_name: table_info.database_name.clone(),
        version: table_info.version,
        columns: table_info.columns.clone(),
        is_clustered: table_info.is_clustered,
        is_partition: true,
        logical_table_id: table_info.table_id,
    };

    let partition_index_info = IndexInfo {
        keyspace_id: table_info.keyspace_id,
        table_id: def.table_id,
        index_id: index_info.index_id,
        index_name: index_info.index_name.clone(),
        index_type: index_info.index_type,
        columns: index_info.columns.clone(),
        is_unique: index_info.is_unique,
        parser_info: index_info.parser_info.clone(),
        other_params: index_info.other_params.clone(),
        fulltext_index: index_info.fulltext_index.clone(),
        hybrid_index: index_info.hybrid_index.clone(),
        created_ts: index_info.created_ts,
    };

    (partition_table_info, partition_index_info)
}

pub fn tipb_column_info_to_field_type(col: &tipb::ColumnInfo) -> tipb::FieldType {
    let mut field_type = tipb::FieldType::default();
    field_type.set_tp(col.get_tp());
    field_type.set_flag(col.get_flag() as u32);
    field_type.set_flen(col.get_column_len());
    field_type.set_decimal(col.get_decimal());
    field_type.set_collate(col.get_collation());
    field_type.set_elems(col.get_elems().to_vec().into());
    field_type.set_array(col.get_array());
    field_type
}
