using BusinessLayer.IRepository;
using BusinessLayer.Mapping;
using BusinessLayer.Repository;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.OData;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using System.Text;
using System.Text.Json.Serialization;

namespace API
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add-start

            builder.Services.AddSwaggerGen();
            builder.Services.AddControllers().AddOData(options =>
            {
                options.Filter().Select().OrderBy().Expand().SkipToken().Count();
            });
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.
                AddSwaggerGen(x =>
                {
                    x.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                    {
                        Description = "JWT Authorization",
                        Name = "Authorization",
                        In = ParameterLocation.Header,
                        Type = SecuritySchemeType.ApiKey,
                        Scheme = "Bearer"
                    });
                    x.AddSecurityRequirement(new OpenApiSecurityRequirement
                    {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id = "Bearer"
                            }
                        },
                        new string[] {}
                    }
                    });
                });
            builder.Services.AddCors(options =>
            {
                options.AddDefaultPolicy(builder =>
                {
                    builder.AllowAnyOrigin()
                           .AllowAnyMethod()
                           .AllowAnyHeader();
                });
            });

            builder.Services.AddAutoMapper(typeof(MappingProfile));
            builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

            builder.Services.AddSingleton<IUserRepository, UserRepository>();
            builder.Services.AddSingleton<ISubjectCategoryRepository, SubjectCategoryRepository>();
            builder.Services.AddSingleton<ISubjectRepository, SubjectRepository>();
            builder.Services.AddSingleton<IQuestionCategoryRepository, QuestionCategoryRepository>();
            builder.Services.AddSingleton<IQuestionRepository, QuestionRepository>();
            builder.Services.AddSingleton<IExamRepository, ExamRepository>();
            builder.Services.AddSingleton<IExamQuestionRepository, ExamQuestionRepository>();


            // Add-end

            builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("AppSettings"));
            var secretKey = builder.Configuration["AppSettings:SecretKey"];
            var secretKeyByte = Encoding.UTF8.GetBytes(secretKey);

            builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(o => o.TokenValidationParameters = new TokenValidationParameters
                {

                    ValidateIssuer = false,
                    ValidateAudience = false,

                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = new SymmetricSecurityKey(secretKeyByte),
                    ClockSkew = TimeSpan.Zero
                });

            var app = builder.Build();

            // Add-start

            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }
            app.MapControllers();

            app.UseAuthentication();

            app.UseAuthorization();

            app.UseCors();

            // Add-end

            //app.MapGet("/", () => "Hello World!");

            app.Run();
        }
    }
}