using Microsoft.EntityFrameworkCore;
using PowderCoating.Core.Entities;
using PowderCoating.Core.Interfaces.Repositories;
using PowderCoating.Infrastructure.Data;

namespace PowderCoating.Infrastructure.Repositories;

/// <summary>
/// Typed repository for <see cref="JobTemplate"/> that provides domain-specific multi-level
/// include queries that the generic <see cref="Repository{T}"/> cannot express.
/// The base class handles all standard CRUD operations; this class adds the read queries
/// that require ThenInclude chains for items, coats, and prep services.
/// </summary>
public class JobTemplateRepository : Repository<JobTemplate>, IJobTemplateRepository
{
    public JobTemplateRepository(ApplicationDbContext context) : base(context) { }

    /// <inheritdoc/>
    public async Task<JobTemplate?> LoadForDetailsAsync(int id)
    {
        return await _context.JobTemplates
            .Where(t => t.Id == id && !t.IsDeleted)
            .Include(t => t.Customer)
            .Include(t => t.Items.Where(i => !i.IsDeleted))
                .ThenInclude(i => i.Coats.Where(c => !c.IsDeleted))
                    .ThenInclude(c => c.InventoryItem)
            .Include(t => t.Items.Where(i => !i.IsDeleted))
                .ThenInclude(i => i.PrepServices.Where(p => !p.IsDeleted))
                    .ThenInclude(p => p.PrepService)
            .FirstOrDefaultAsync();
    }

    /// <inheritdoc/>
    public async Task<List<JobTemplate>> GetAllActiveWithFullIncludesAsync()
    {
        return await _context.JobTemplates
            .Where(t => !t.IsDeleted && t.IsActive)
            .Include(t => t.Customer)
            .Include(t => t.Items.Where(i => !i.IsDeleted))
                .ThenInclude(i => i.Coats.Where(c => !c.IsDeleted))
            .Include(t => t.Items.Where(i => !i.IsDeleted))
                .ThenInclude(i => i.PrepServices.Where(p => !p.IsDeleted))
                    .ThenInclude(p => p.PrepService)
            .OrderBy(t => t.Name)
            .ToListAsync();
    }
}
