using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using DataServer.Data; using DataServer.Data.Entities; namespace GameServices.Admin.Pages.Recipes; public class EditModel : AdminPageModel { private readonly GameDbContext _db; public EditModel(GameDbContext db) { _db = db; } [BindProperty] public DungeonCraftingRecipe Recipe { get; set; } = new(); public List Ingredients { get; set; } = new(); public List AllItems { get; set; } = new(); public bool IsNew => Recipe.Id == 0; public string? Message { get; set; } public bool IsError { get; set; } public string CdnBaseUrl { get; set; } = "https://assets.aeternalisgames.com"; public async Task OnGetAsync(int? id) { var cdnUrl = Environment.GetEnvironmentVariable("CDN_BASE_URL"); if (!string.IsNullOrEmpty(cdnUrl)) CdnBaseUrl = cdnUrl; AllItems = await _db.DungeonItemDefinitions .Where(i => i.IsEnabled) .OrderBy(i => i.Type) .ThenBy(i => i.Name) .ToListAsync(); if (id.HasValue) { var recipe = await _db.DungeonCraftingRecipes .Include(r => r.OutputItem) .FirstOrDefaultAsync(r => r.Id == id.Value); if (recipe != null) { Recipe = recipe; Ingredients = await _db.DungeonCraftingRecipeIngredients .Include(i => i.ItemDefinition) .Where(i => i.RecipeId == id.Value) .OrderBy(i => i.SlotOrder) .ToListAsync(); } } else { Recipe.IsEnabled = true; Recipe.RequiredLevel = 1; Recipe.Tier = 1; Recipe.OutputQuantity = 1; Recipe.BaseXp = 25; } } public async Task OnPostAsync() { var cdnUrl = Environment.GetEnvironmentVariable("CDN_BASE_URL"); if (!string.IsNullOrEmpty(cdnUrl)) CdnBaseUrl = cdnUrl; AllItems = await _db.DungeonItemDefinitions.Where(i => i.IsEnabled).OrderBy(i => i.Name).ToListAsync(); Recipe.IsEnabled = Request.Form.ContainsKey("Recipe.IsEnabled"); Recipe.IsBaseRecipe = Request.Form.ContainsKey("Recipe.IsBaseRecipe"); Recipe.HasStatRolls = Request.Form.ContainsKey("Recipe.HasStatRolls"); // Ensure Description is never null Recipe.Description ??= ""; if (string.IsNullOrWhiteSpace(Recipe.Name)) { Message = "Name is required."; IsError = true; return Page(); } if (Recipe.OutputItemDefinitionId == 0) { Message = "Output item is required."; IsError = true; return Page(); } try { if (Recipe.Id == 0) { _db.DungeonCraftingRecipes.Add(Recipe); await _db.SaveChangesAsync(); Message = $"Recipe '{Recipe.Name}' created. Now add ingredients below."; return RedirectToPage("/Recipes/Edit", new { id = Recipe.Id }); } else { var existing = await _db.DungeonCraftingRecipes.FindAsync(Recipe.Id); if (existing == null) { Message = "Recipe not found."; IsError = true; return Page(); } existing.Name = Recipe.Name; existing.Description = Recipe.Description ?? ""; existing.SkillType = Recipe.SkillType; existing.RequiredLevel = Recipe.RequiredLevel; existing.OutputItemDefinitionId = Recipe.OutputItemDefinitionId; existing.OutputQuantity = Recipe.OutputQuantity; existing.Tier = Recipe.Tier; existing.BaseXp = Recipe.BaseXp; existing.IsBaseRecipe = Recipe.IsBaseRecipe; existing.HasStatRolls = Recipe.HasStatRolls; existing.IsEnabled = Recipe.IsEnabled; await _db.SaveChangesAsync(); Recipe = await _db.DungeonCraftingRecipes .Include(r => r.OutputItem) .FirstAsync(r => r.Id == existing.Id); Ingredients = await _db.DungeonCraftingRecipeIngredients .Include(i => i.ItemDefinition) .Where(i => i.RecipeId == Recipe.Id) .OrderBy(i => i.SlotOrder) .ToListAsync(); Message = "Changes saved successfully."; } } catch (Exception ex) { Message = $"Error: {ex.Message}"; IsError = true; } return Page(); } public async Task OnPostAddIngredientAsync(int recipeId, int itemId, int slotOrder, int quantity) { var ingredient = new DungeonCraftingRecipeIngredient { RecipeId = recipeId, ItemDefinitionId = itemId, SlotOrder = slotOrder, Quantity = quantity }; _db.DungeonCraftingRecipeIngredients.Add(ingredient); await _db.SaveChangesAsync(); return RedirectToPage("/Recipes/Edit", new { id = recipeId }); } public async Task OnPostRemoveIngredientAsync(int ingredientId, int recipeId) { var ingredient = await _db.DungeonCraftingRecipeIngredients.FindAsync(ingredientId); if (ingredient != null) { _db.DungeonCraftingRecipeIngredients.Remove(ingredient); await _db.SaveChangesAsync(); } return RedirectToPage("/Recipes/Edit", new { id = recipeId }); } }