using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Quartz;
using System;
using System.Linq;
using wispro.sp.api.Job;

namespace wispro.sp.api
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var serviceScope = host.Services.CreateScope())
            {
                var services = serviceScope.ServiceProvider;

                try
                {
                    var context = services.GetRequiredService<spDbContext>();

                    var result = context.Database.EnsureCreated();

                    if (result)
                    {
                        System.Diagnostics.Debug.Write("数据库创建成功！");
                    }
                    else
                    {
                        System.Diagnostics.Debug.Write("数据库创建失败！");
                    }

                }
                catch (Exception ex)
                {
                    //var logger = services.GetRequiredService<ILogger<Program>>();
                    //logger.LogError(ex, "An error occurred.");
                }
            }
            

            JobKey jobKey = new JobKey("ImportReportData");
            var trigger = TriggerBuilder.Create()
                .WithDescription("导入每月报表")
                .WithSchedule(CronScheduleBuilder.CronSchedule(utility.ConfigHelper.GetSectionValue("IPEasySetting:ScheduleSetting")).WithMisfireHandlingInstructionDoNothing())
                .Build();


            QuartzUtil.Add(typeof(ImportReportJob), jobKey, trigger);
            

            host.Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

        
    }
}
