using Meezi.Core.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace Meezi.Infrastructure.Data;

public static class DemoEmployeesSeeder
{
    public static async Task EnsureEmployeesAsync(AppDbContext db, string cafeId, ILogger logger)
    {
        var existingPhones = await db.Employees
            .Where(e => e.CafeId == cafeId && e.DeletedAt == null)
            .Select(e => e.Phone)
            .ToListAsync();

        var added = 0;
        foreach (var seed in DemoEmployeesCatalog.Employees)
        {
            if (existingPhones.Contains(seed.Phone))
                continue;

            if (await db.Employees.AnyAsync(e => e.Id == seed.Id, cancellationToken: default))
                continue;

            db.Employees.Add(new Employee
            {
                Id = seed.Id,
                CafeId = cafeId,
                BranchId = seed.BranchId,
                Name = seed.Name,
                Phone = seed.Phone,
                Role = seed.Role,
                BaseSalary = seed.BaseSalary
            });
            added++;
        }

        if (added > 0)
        {
            await db.SaveChangesAsync();
            logger.LogInformation("Demo employees seed: {Count} added for cafe {CafeId}", added, cafeId);
        }
    }
}
