using DataLayer;
using DataLayer.Interfaces;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Services;
using Services.Interfaces;
using Microsoft.EntityFrameworkCore;

namespace Warhammer;

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        //services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
        //{
        //    builder.AllowAnyOrigin()
        //        .AllowAnyMethod()
        //        .AllowAnyHeader();
        //}));

        services.AddMvc();
        services.AddHttpClient();
        services.AddControllersWithViews();
        services.AddRazorPages();

        services.AddDbContext<DeathwatchDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddTransient(typeof(IAbstractRepository<>), typeof(AbstractRepository<>));
        services.AddTransient<IPlayerService, PlayerService>();
        services.AddTransient<IPlayerRepository, PlayerRepository>();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        //if (env.IsDevelopment())
        //{
        //    app.UseDeveloperExceptionPage();
        //}
        //else
        //{
        //    app.UseExceptionHandler("/Home/Error");
        //    //The default HSTS value is 30 days.You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        //}

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

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                "default",
                "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
        });

        app.UseHsts();
    }
}