using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace sotd.Pages;

public class SuggestionHelpersModel : PageModel
{
    private readonly ILogger<SuggestionHelpersModel> _logger;

    public SuggestionHelpersModel(ILogger<SuggestionHelpersModel> logger)
    {
        _logger = logger;
        this.NewSuggestionDescription = "";
        this.NewSuggestionTitle = "";
        this.SuggestionHelpers = new List<SuggestionHelper>();
    }

    [BindProperty]
    public ICollection<SuggestionHelper> SuggestionHelpers { get; set; }

    [BindProperty]
    public string NewSuggestionTitle { get; set; }

    [BindProperty]
    public string NewSuggestionDescription { get; set; }

    public void OnGet()
    {
        using (var dci = DataContext.Instance)
        {
            this.SuggestionHelpers = dci.SuggestionHelpers == null ? new List<SuggestionHelper>() : dci.SuggestionHelpers.ToList();
        }
    }

    public void OnPost()
    {
        using (var dci = DataContext.Instance)
        {
            var newHelper = new SuggestionHelper()
            {
                Title = this.NewSuggestionTitle,
                Description = this.NewSuggestionDescription
            };
            dci.SuggestionHelpers?.Add(newHelper);
            dci.SaveChanges();
            this.SuggestionHelpers = dci.SuggestionHelpers == null ? new List<SuggestionHelper>() : dci.SuggestionHelpers.ToList();
        }
        this.NewSuggestionDescription = "";
        this.NewSuggestionTitle = "";
    }
}
