using Back;
using Back.Data.Contracts;
using Back.Data.Infrastructure.Repository;
using Back.Features;
using Back.Services;
using Back.Validations;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.Net.Http.Headers;
using Microsoft.OpenApi.Models;
using System.Text;
using TaxPayer.Infrastructure.Persistence;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(option =>
{
    option.SwaggerDoc("v1", new OpenApiInfo { Title = "TaxPayer API", Version = "Preview" });
    option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
    {
        In = ParameterLocation.Header,
        Description = "Please enter a valid token",
        Name = "Authorization",
        Type = SecuritySchemeType.Http,
        BearerFormat = "JWT",
        Scheme = "Bearer"
    });
    option.AddSecurityRequirement(new OpenApiSecurityRequirement
    {
        {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                {
                    Type=ReferenceType.SecurityScheme,
                    Id="Bearer"
                }
            },
            new string[]{}
        }
    });

});
builder.Services.AddDbContext<SqlDbContext>(options =>
{
    options.UseSqlServer(builder.Configuration.GetConnectionString("Base"));
});
builder.Services.AddScoped(typeof(IAsyncRepository<>), typeof(RepositoryBase<>));
builder.Services.AddScoped(typeof(RepositoryBase<>), typeof(RepositoryBase<>));
builder.Services.AddScoped<Back.Services.ServBase>();
builder.Services.AddScoped<MobileValidation> ();
builder.Services.AddScoped<Service.Main>();
builder.Services.AddScoped<servTicket > ();
builder.Services.AddScoped<ServValidatinMsg>();
builder.Services.AddScoped<GetVerificationValidation> ();
builder.Services.AddScoped<GetVerificationValidation>();
builder.Services.AddScoped<CheckPermission>();
builder.Services.AddScoped<servCompany>();
builder.Services.AddScoped<servNotification>();
builder.Services.AddScoped<servPermission>();
builder.Services.AddScoped<servSendMsg>();
builder.Services.AddScoped<servUser>();
builder.Services.AddScoped<servTaxPayer>();
builder.Services.AddScoped<servCustomer>();
builder.Services.AddScoped<CompanyRegistrationValidation>();
builder.Services.AddScoped<AddOrCustomerValidation>();
builder.Services.AddScoped<AddOrCodValidation>();
builder.Services.AddScoped<ServCOD>();
builder.Services.AddScoped<servInvoice>();
builder.Services.AddScoped<AddOrUpdateInvoiceValidation>();
builder.Services.AddScoped<AUInvoiceItemValidation>();
builder.Services.AddScoped<servInvoiceItem>();
builder.Services.AddScoped<ActionTaxPayer>();
builder.Services.AddScoped(c => new mpNuget.RestClient("09119660045", "C54S2"));

string origins = "OriginTaxPayer";
builder.Services.AddCors(options =>
{
    options.AddPolicy(origins,
                          policy =>
                          {
                              policy.WithOrigins("https://localhost:7224", "http://localhost:5107")
                                                  .AllowAnyHeader()
                                                    .WithHeaders(HeaderNames.ContentType)
                                                  .AllowAnyMethod();
                          });
});

#region JWT
builder.Services.AddAuthentication("Bearer")
    .AddJwtBearer(options =>
    {
        options.TokenValidationParameters = new()
        {
            ValidateIssuer = true,
            ValidateAudience = true,
            ValidateIssuerSigningKey = true,
            ValidIssuer = Fixedvalues.Issuer,
            ValidAudience = Fixedvalues.Audience,
            IssuerSigningKey = new SymmetricSecurityKey(
                Encoding.ASCII.GetBytes(Fixedvalues.SecretForKey))
        };
    }
    );
#endregion

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseCors(origins);
app.UseAuthentication();
app.UseAuthorization();
app.UseCheckOnlineUser();
app.MapControllers();

app.Run();
