using Microsoft.EntityFrameworkCore;
using MiGu.Server.Persistence;
using MiGu.DB.Domains.SimpleFields;

namespace MiGu.Server.SimpleFields;

public sealed class SimpleFieldService
{
    private readonly PlatformDbContext _db;

    public SimpleFieldService(PlatformDbContext db) => _db = db;

    public async Task<List<SimpleField>> ListAsync(string? fieldType = null, string? carType = null, string? q = null)
    {
        var query = _db.SimpleFields.AsNoTracking()
            .OrderBy(x => x.CarType).ThenBy(x => x.FieldType).ThenBy(x => x.Key)
            .AsQueryable();
        if (!string.IsNullOrWhiteSpace(fieldType))
            query = query.Where(x => x.FieldType == fieldType);
        if (!string.IsNullOrWhiteSpace(carType))
            query = query.Where(x => x.CarType == carType);
        if (!string.IsNullOrWhiteSpace(q))
        {
            var kw = q.Trim();
            query = query.Where(x =>
                x.Key.Contains(kw) ||
                x.CarType.Contains(kw) ||
                (x.Chinese != null && x.Chinese.Contains(kw)) ||
                (x.English != null && x.English.Contains(kw)) ||
                x.Other.Contains(kw) ||
                x.Value.Contains(kw));
        }
        return await query.ToListAsync();
    }

    public async Task<SimpleField> SaveAsync(SimpleFieldRequest req)
    {
        if (string.IsNullOrWhiteSpace(req.CarType)) throw new SimpleFieldException("car_type 不能为空");
        if (string.IsNullOrWhiteSpace(req.FieldType)) throw new SimpleFieldException("field_type 不能为空");
        if (string.IsNullOrWhiteSpace(req.Key)) throw new SimpleFieldException("key 不能为空");

        var carType = req.CarType.Trim();
        var now = SimpleFieldDateTime.Now;
        SimpleField entity;
        if (req.Id is { } id && id != Guid.Empty)
        {
            entity = await _db.SimpleFields.FirstOrDefaultAsync(x => x.Id == id)
                     ?? throw new SimpleFieldException("记录不存在");
        }
        else
        {
            var dup = await _db.SimpleFields.AnyAsync(x =>
                x.CarType == carType &&
                x.FieldType == req.FieldType.Trim() &&
                x.Key == req.Key.Trim());
            if (dup) throw new SimpleFieldException("同车型与字段类型下 key 已存在");

            entity = new SimpleField { Id = Guid.NewGuid(), CreateTime = now };
            _db.SimpleFields.Add(entity);
        }

        entity.CarType = carType;
        entity.FieldType = req.FieldType.Trim();
        entity.Key = req.Key.Trim();
        entity.Value = req.Value?.Trim() ?? "";
        entity.DataType = req.DataType?.Trim() ?? "";
        entity.Chinese = req.Chinese is null ? null : req.Chinese.Trim();
        entity.English = req.English is null ? null : req.English.Trim();
        entity.Other = req.Other?.Trim() ?? "";
        entity.IsDefault = req.IsDefault;
        entity.UpdateTime = now;
        if (entity.CreateTime == default) entity.CreateTime = now;

        await _db.SaveChangesAsync();
        return entity;
    }

    public async Task DeleteAsync(Guid id)
    {
        var entity = await _db.SimpleFields.FirstOrDefaultAsync(x => x.Id == id)
                     ?? throw new SimpleFieldException("记录不存在");
        _db.SimpleFields.Remove(entity);
        await _db.SaveChangesAsync();
    }

    /// <summary>批量保存全部字段；ReplaceAll=true 时清空表后写入。</summary>
    public async Task<int> SaveBatchAsync(SimpleFieldBatchRequest req)
    {
        var items = req.Items ?? new List<SimpleFieldRequest>();
        if (items.Count == 0) throw new SimpleFieldException("没有可保存的字段");

        if (req.ReplaceAll)
        {
            var all = await _db.SimpleFields.ToListAsync();
            _db.SimpleFields.RemoveRange(all);
        }

        var now = SimpleFieldDateTime.Now;
        var added = 0;
        foreach (var item in items)
        {
            if (string.IsNullOrWhiteSpace(item.CarType) ||
                string.IsNullOrWhiteSpace(item.FieldType) ||
                string.IsNullOrWhiteSpace(item.Key))
                continue;

            _db.SimpleFields.Add(new SimpleField
            {
                Id = Guid.NewGuid(),
                CarType = item.CarType.Trim(),
                FieldType = item.FieldType.Trim(),
                Key = item.Key.Trim(),
                Value = item.Value?.Trim() ?? "",
                DataType = item.DataType?.Trim() ?? "",
                Chinese = item.Chinese is null ? null : item.Chinese.Trim(),
                English = item.English is null ? null : item.English.Trim(),
                Other = item.Other?.Trim() ?? "",
                IsDefault = item.IsDefault,
                CreateTime = now,
                UpdateTime = now
            });
            added++;
        }

        await _db.SaveChangesAsync();
        return added;
    }
}

public sealed class SimpleFieldException : Exception
{
    public SimpleFieldException(string message) : base(message) { }
}
