using Microsoft.EntityFrameworkCore;
using MiniFullStackApi.Models;  

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more abo ut configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle


builder.Services.AddDbContext<AppDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

/*
using (var scope = app.Services.CreateScope())
{
    var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();

    // Optional: ensures db is created.
    db.Database.EnsureCreated();

    if (!db.Customers.Any())
    {
        // Seed some data if the database is empty.
        db.Customers.AddRange(
            new Customer { Name = "Alice", Region = "North" },
            new Customer { Name = "Bob", Region = "South" },
            new Customer { Name = "Charlie", Region = "East" }
        );
        db.SaveChanges();
    }

    // List existing customers OR count them
    var customerCount = db.Customers.Count();
    Console.WriteLine($"There are {customerCount} customers in the database.");


    Console.WriteLine("All customers:");
    foreach (var customer in db.Customers)    {
        Console.WriteLine($"- Id: {customer.Id}, Name: {customer.Name} from {customer.Region}");
    }


    // ADD a new customer

    var newCustomer = new Customer
    {
        Name = "Diana",
        Region = "West"
    };

    db.Customers.Add(newCustomer);
    db.SaveChanges();

    Console.WriteLine($"Added new customer: Id: {newCustomer.Id}, Name: {newCustomer.Name} from {newCustomer.Region}");

    Console.WriteLine("\nAll customers:");
    foreach (var customer in db.Customers)
    {
        Console.WriteLine($"- Id: {customer.Id}, Name: {customer.Name} from {customer.Region}");
    }


    // UPDATE a customer
    var customerToUpdate = db.Customers.FirstOrDefault(c => c.Name == "Alice");
    if (customerToUpdate != null)
    {
        customerToUpdate.Region = "Central";
        db.SaveChanges();
        Console.WriteLine($"Updated customer: Id: {customerToUpdate.Id}, Name: {customerToUpdate.Name} now from {customerToUpdate.Region}");
    }

     Console.WriteLine("\nAll customers:");
    foreach (var customer in db.Customers)
    {
        Console.WriteLine($"- Id: {customer.Id}, Name: {customer.Name} from {customer.Region}");
    }

    var customerToDelete = db.Customers.FirstOrDefault(c => c.Name == "Diana");
    if (customerToDelete != null)
    {
        db.Customers.Remove(customerToDelete);
        db.SaveChanges();

        Console.WriteLine($"\nDeleted customer: Id: {customerToDelete.Id}, Name: {customerToDelete.Name} from {customerToDelete.Region}");
    }

}
*/

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

app.UseHttpsRedirection();

app.UseAuthorization();

// app.MapControllers();

// Get all customers
app.MapGet("/customers", async (AppDbContext db) =>
{
    return await db.Customers.Include(c => c.Orders).ToListAsync();
});

// Get a single customer by ID
app.MapGet("/customers/{id:int}", async (int id, AppDbContext db) =>
{
    var customer = await db.Customers.Include(c => c.Orders).FirstOrDefaultAsync(c => c.Id == id);
    return customer is not null ? Results.Ok(customer) : Results.NotFound();
});

// Create a new customer
app.MapPost("/customers", async (Customer customer, AppDbContext db) =>
{
    db.Customers.Add(customer);
    await db.SaveChangesAsync();
    return Results.Created($"/customers/{customer.Id}", customer);
});

// Update an existing customer
app.MapPut("/customers/{id:int}", async (int id, Customer updatedCustomer, AppDbContext db) =>
{
    var customer = await db.Customers.FindAsync(id);
    if (customer is null) return Results.NotFound();

    customer.Name = updatedCustomer.Name;
    customer.Region = updatedCustomer.Region;
    await db.SaveChangesAsync();

    return Results.Ok(customer);
});

// Delete a customer
app.MapDelete("/customers/{id:int}", async (int id, AppDbContext db) =>
{
    var customer = await db.Customers.FindAsync(id);
    if (customer is null) return Results.NotFound();

    db.Customers.Remove(customer);
    await db.SaveChangesAsync();

    // return Results.NoContent();
    return Results.Ok(customer);
});




app.Run();
