using System;
namespace Apewer.Source
{
    /// 索引。
    public sealed class TableIndex
    {
        /// 索引名称。
        public string Name { get; }
        /// 表。
        public TableAttribute Table { get; }
        /// 索引字段。
        public IndexAttribute[] Fields { get; }
        /// 创建索引实例。
        /// 
        internal TableIndex(string name, TableAttribute table, IndexAttribute[] fields)
        {
            if (name.IsEmpty()) throw new ArgumentNullException(nameof(name));
            if (table == null) throw new ArgumentNullException(nameof(table));
            if (fields == null) throw new ArgumentNullException(nameof(fields));
            Name = name;
            Table = table;
            Fields = fields;
        }
        /// 
        public override string ToString()
        {
            var fields = Fields.FindAll(x => x != null && x.Column != null);
            if (fields.IsEmpty()) return Name;
            var array = fields.Map(x => x.Order == FieldOrder.Ascend ? x.Column.Field : $"{x.Column.Field} DESC");
            return $"{Name} ({array.Join(", ")})";
        }
    }
}