using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PARR.DAL.Context;
using PARR.DAL.Services.Abstracts;
using PARR.DAL.Services.Interfaces.Job;

namespace PARR.DAL.Services.Implementations.Job
{
    internal class JobService : BaseService<Models.Job.Job>, IJobService
    {
        private readonly DataContext dataContext;

        public JobService(DataContext dataContext, ILogger<JobService> logger) : base(logger)
        {
            this.dataContext = dataContext;
        }

        protected override DbSet<Models.Job.Job> EntitySet => dataContext.Jobs;

        protected override DataContext EntitiContext => dataContext;

        public override Task<bool> CreateAsync(Models.Job.Job obj)
        {
            if (obj.AutoControl == null)
                obj.AutoControl = new Models.Job.JobAutoControl
                {
                    JobId = obj.Id
                };

            return base.CreateAsync(obj);
        }


        public override Task<bool> AddRangeAsync(List<Models.Job.Job> objs)
        {
            objs.ForEach(job =>
            {
                if (job.AutoControl == null)
                    job.AutoControl = new Models.Job.JobAutoControl
                    {
                        JobId = job.Id
                    };
            });

            return base.AddRangeAsync(objs);
        }
    }
}
