using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PARR.Core.Repositories.Interfaces;
using PARR.Domain.Entities;

namespace PARR.MockData.Services
{
    internal class TemplatesInitializer : ITemplatesInitializer
    {
        private readonly ILogger<TemplatesInitializer> logger;
        private readonly ITemplateRepository templateService;
        private readonly IProcessRepository processService;
        private readonly ISubprocessRepository subprocessService;
        private readonly ITnkRepository tnkService;
        //private readonly IWorkService workService;

        public TemplatesInitializer(
            ILogger<TemplatesInitializer> logger,
            ITemplateRepository templateService,
            IProcessRepository processService,
            ISubprocessRepository subprocessService,
            ITnkRepository tnkService
            //IWorkService workService
            )
        {
            this.logger = logger;
            this.templateService = templateService;
            this.processService = processService;
            this.subprocessService = subprocessService;
            this.tnkService = tnkService;
            //this.workService = workService;
        }
        public async Task InitAsync()
        {
            var process = await processService.Get().FirstOrDefaultAsync(p => p.EsppId == 202);
            if (process == null)
            {
                process = new Process
                {
                    Id = Guid.NewGuid(),
                    DateCreated = DateTimeOffset.UtcNow,
                    Name = "ПТК Выполнение регламентных работ (О3.2.2.4)",
                    EsppId = 202,
                };
                if (!await processService.CreateAsync(process) || !await processService.CommitAsync())
                {
                    logger.LogError($"Не удалось создать process {process.Name}");
                    return;
                }
                else
                    logger.LogInformation($"----- Создан process: {process.Name}  -----");
            }

            var subprocess = await subprocessService.Get().FirstOrDefaultAsync(p => p.EsppId == 202);
            if (subprocess == null)
            {
                subprocess = new Subprocess
                {
                    Id = Guid.NewGuid(),
                    DateCreated = DateTimeOffset.UtcNow,
                    Name = "ПТК Выполнение регламентных работ (О3.2.2.4)",
                    EsppId = 202,
                    ProcessId = process.Id,
                };
                if (!await subprocessService.CreateAsync(subprocess) || !await subprocessService.CommitAsync())
                {
                    logger.LogError($"Не удалось создать subprocess {subprocess.Name}");
                    return;
                }
                else
                    logger.LogInformation($"----- Создан subprocess: {subprocess.Name}  -----");
            }

            var tnk = await tnkService.Get().FirstOrDefaultAsync(p => p.EsppId == 292917);
            if (tnk == null)
            {
                tnk = new Tnk
                {
                    Id = Guid.NewGuid(),
                    DateCreated = DateTimeOffset.UtcNow,
                    Name = "Прочее(работы)",
                    EsppId = 292917,
                    SubprocessId = subprocess.Id
                };
                if (!await tnkService.CreateAsync(tnk) || !await tnkService.CommitAsync())
                {
                    logger.LogError($"Не удалось создать tnk {tnk.Name}");
                    return;
                }
                else
                    logger.LogInformation($"----- Создан tnk: {tnk.Name}  -----");
            }

            //var work = await workService.Get().FirstOrDefaultAsync(p => p.EsppId == 292917);
            //if (work == null)
            //{
            //    work = new Work
            //    {
            //        Id = Guid.NewGuid(),
            //        DateCreated = DateTimeOffset.UtcNow,
            //        Name = "Прочее(работы)",
            //        EsppId = 292917,
            //        TnkId = tnk.Id,
            //        TemplateNameMask= "%ЗО%-ЭИТИ-ПТК-ПАРР__%ЭК%__%РАБОТА%"
            //    };
            //    if (!await workService.CreateAsync(work) || !await workService.CommitAsync())
            //    {
            //        logger.LogError($"Не удалось создать work {work.Name}");
            //        return;
            //    }
            //    else
            //        logger.LogInformation($"----- Создан work: {work.Name}  -----");
            //}
        }

    }
}
