using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using MiGu.DB.Domains.Dashboard;
using MiGu.DB.Domains.SimpleFields;
using MiGu.DB.Domains.Transport;

namespace MiGu.DB.Domains.Transport
{
    public sealed class WmsTransportRuleConfiguration : IEntityTypeConfiguration<WmsTransportRule>
    {
        public void Configure(EntityTypeBuilder<WmsTransportRule> b)
        {
            b.ToTable("wms_transport_rules");
            b.HasKey(x => x.Id);
            b.HasIndex(x => x.Code).IsUnique();
            b.HasIndex(x => new { x.TriggerType, x.Enabled, x.Priority });
            b.Property(x => x.SourceSelectorJson).HasColumnType("text");
            b.Property(x => x.TargetSelectorJson).HasColumnType("text");
            b.Property(x => x.TaskOptionsJson).HasColumnType("text");
        }
    }

    public sealed class WmsTransportTaskConfiguration : IEntityTypeConfiguration<WmsTransportTask>
    {
        public void Configure(EntityTypeBuilder<WmsTransportTask> b)
        {
            b.ToTable("wms_transport_tasks");
            b.HasKey(x => x.Id);
            b.HasIndex(x => x.Status);
            b.HasIndex(x => x.ContainerId);
            b.HasIndex(x => x.TargetStorageId);
            b.Property(x => x.Quantity).HasPrecision(18, 4);
            b.Property(x => x.SnapshotJson).HasColumnType("text");
        }
    }

    public sealed class WmsTransportReservationConfiguration : IEntityTypeConfiguration<WmsTransportReservation>
    {
        public void Configure(EntityTypeBuilder<WmsTransportReservation> b)
        {
            b.ToTable("wms_transport_reservations");
            b.HasKey(x => x.Id);
            b.HasIndex(x => new { x.ContainerId, x.Status });
            b.HasIndex(x => new { x.TargetStorageId, x.Status });
        }
    }

    public sealed class WmsTransportTaskHistoryConfiguration : IEntityTypeConfiguration<WmsTransportTaskHistory>
    {
        public void Configure(EntityTypeBuilder<WmsTransportTaskHistory> b)
        {
            b.ToTable("wms_transport_task_history");
            b.HasKey(x => x.Id);
            b.Property(x => x.FromStatus).HasMaxLength(32);
            b.Property(x => x.ToStatus).HasMaxLength(32);
            b.Property(x => x.Operator).HasMaxLength(128);
            b.Property(x => x.Reason).HasMaxLength(500);
            b.Property(x => x.ErrorMessage).HasMaxLength(1000);
            b.Property(x => x.SnapshotJson).HasColumnType("text");
            b.HasIndex(x => x.TaskId);
            b.HasIndex(x => x.OperatedAt);
        }
    }
}

namespace MiGu.DB.Domains.SimpleFields
{
    public sealed class SimpleFieldConfiguration : IEntityTypeConfiguration<SimpleField>
    {
        public void Configure(EntityTypeBuilder<SimpleField> b)
        {
            b.ToTable("simple_fields");
            b.HasKey(x => x.Id);
            b.Property(x => x.Id).HasColumnName("id");
            b.Property(x => x.CarType).HasColumnName("car_type").HasMaxLength(64);
            b.Property(x => x.FieldType).HasColumnName("field_type").HasMaxLength(64);
            b.Property(x => x.Key).HasColumnName("key").HasMaxLength(128);
            b.Property(x => x.Value).HasColumnName("value");
            b.Property(x => x.DataType).HasColumnName("data_type").HasMaxLength(128);
            b.Property(x => x.Chinese).HasColumnName("chinese").HasMaxLength(256).IsRequired(false);
            b.Property(x => x.English).HasColumnName("english").HasMaxLength(256).IsRequired(false);
            b.Property(x => x.Other).HasColumnName("other").HasMaxLength(512);
            b.Property(x => x.IsDefault).HasColumnName("is_default");
            var dateTime = new ValueConverter<DateTimeOffset, string>(
                v => SimpleFieldDateTime.ToStorage(v),
                v => SimpleFieldDateTime.FromStorage(v));
            b.Property(x => x.CreateTime).HasColumnName("create_time").HasConversion(dateTime).HasMaxLength(19);
            b.Property(x => x.UpdateTime).HasColumnName("update_time").HasConversion(dateTime).HasMaxLength(19);
            b.HasIndex(x => new { x.CarType, x.FieldType, x.Key }).IsUnique();
        }
    }
}

namespace MiGu.DB.Domains.Dashboard
{
    public sealed class UserDashboardShortcutConfiguration : IEntityTypeConfiguration<UserDashboardShortcut>
    {
        public void Configure(EntityTypeBuilder<UserDashboardShortcut> b)
        {
            b.ToTable("user_dashboard_shortcuts");
            b.HasKey(x => new { x.UserId, x.Scope });
            b.Property(x => x.UserId).HasColumnName("user_id").HasMaxLength(64);
            b.Property(x => x.Scope).HasColumnName("scope").HasMaxLength(32);
            b.Property(x => x.KeysJson).HasColumnName("keys_json").HasColumnType("text");
            var dateTime = new ValueConverter<DateTimeOffset, string>(
                v => v.UtcDateTime.ToString("O"),
                v => DateTimeOffset.Parse(v));
            b.Property(x => x.UpdatedAt).HasColumnName("updated_at").HasConversion(dateTime).HasMaxLength(40);
        }
    }
}
