using CouponReport.Models.CouponMiddleware;
using CouponReport.Models.Parkingeyes;
using CouponReport.Service;
using LaneFlowReport.Auth;
using LaneFlowReport.Options;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddHttpClient();
builder.Services.AddScoped<ReportService>();
builder.Services.AddSingleton<ParkingAuthProvider>();

builder.Services.Configure<OauthOption>(builder.Configuration.GetSection("Authentication:Parking"));
builder.Services.AddDbContext<CouponMiddlewareContext>(options =>
	options.UseSqlServer(builder.Configuration.GetConnectionString("myConnection")));

builder.Services.AddDbContext<ParkingEyesContext>(options =>
	options.UseSqlServer(builder.Configuration.GetConnectionString("ParkingEyesConnection")));

builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
	.AddCookie(options =>
	{
		options.LoginPath = "/Home/Index";
		options.AccessDeniedPath = "/Home/Index";
		options.Cookie.Name = "CouponReport.Auth"; // 設定 Cookie 名稱
		options.Cookie.HttpOnly = true;
		options.Cookie.IsEssential = true;
		options.Cookie.SameSite = SameSiteMode.Strict;
		options.SlidingExpiration = true;
		options.ExpireTimeSpan = TimeSpan.FromMinutes(15); // 設定15分鐘後過期
	});

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddSession(options =>
{
	options.Cookie.HttpOnly = true;
	options.Cookie.IsEssential = true;
	options.IdleTimeout = TimeSpan.FromMinutes(15); // Session 15分鐘後過期
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseSession();
app.UseAuthentication();
app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
