using OnlineSalesAutoCrop.WS;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging.EventLog;
using System.Net;

WebApplicationOptions options = new()
{
	Args = args,
	ContentRootPath = AppContext.BaseDirectory
};

var builder = WebApplication.CreateBuilder(options);
builder.Host.UseWindowsService();
builder.Services.AddCors(options =>
{
	options.DefaultPolicyName = "CORSPolicy";
	options.AddDefaultPolicy(builder =>
	{
		builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
	});
});

//Configure App Settings
builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("AppSettings"));

//Configure Services
builder.Services.AddHostedService<WorkerService>();

#pragma warning disable CA1416
builder.Services.Configure<EventLogSettings>(config =>
{
	config.LogName = "O2C_HostedWebService";
	config.SourceName = "O2C_ASP.Net_HostedWebService";
});
#pragma warning restore CA1416

builder.WebHost.ConfigureKestrel((context, serverOptions) =>
{
	serverOptions.Listen(IPAddress.Loopback, 7777);
});
builder.Services.AddMvcCore();

//Configure Application
var app = builder.Build();
app.UseCors("CORSPolicy");
app.UseRouting();
app.MapControllers();
app.Run();