using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using EonaCat.LogStack.Status.Services;

namespace EonaCat.LogStack.Status.Pages.Admin;

// This file is part of the EonaCat project(s) which is released under the Apache License.
// See the LICENSE file or go to https://EonaCat.com/License for full license details.

public class SettingsModel : PageModel
{
    private readonly AuthenticationService _authenticationService;
    public SettingsModel(AuthenticationService authentication) => _authenticationService = authentication;

    [BindProperty] public string SiteName { get; set; } = "";
    [BindProperty] public bool ShowLogsPublicly { get; set; }
    [BindProperty] public bool ShowUptimePublicly { get; set; }
    [BindProperty] public bool ShowIncidentsPublicly { get; set; }
    [BindProperty] public bool AutoCreateIncidents { get; set; }
    [BindProperty] public int MaxLogRetentionDays { get; set; } = 30;
    [BindProperty] public string AlertEmail { get; set; } = "";
    [BindProperty] public string AlertWebhookUrl { get; set; } = "";
    [BindProperty] public string CurrentPassword { get; set; } = "";
    [BindProperty] public string NewPassword { get; set; } = "";
    [BindProperty] public string ConfirmPassword { get; set; } = "";
    public string? Message { get; set; }
    public string? PasswordMessage { get; set; }

    public async Task<IActionResult> OnGetAsync()
    {
        if (HttpContext.Session.GetString("IsAdmin") != "true")
        {
            return RedirectToPage("/Admin/Login");
        }

        SiteName = await _authenticationService.GetSettingAsync("SiteName", "Status");
        ShowLogsPublicly = (await _authenticationService.GetSettingAsync("ShowLogsPublicly", "false")) == "true";
        ShowUptimePublicly = (await _authenticationService.GetSettingAsync("ShowUptimePublicly", "true")) == "true";
        ShowIncidentsPublicly = (await _authenticationService.GetSettingAsync("ShowIncidentsPublicly", "true")) == "true";
        AutoCreateIncidents = (await _authenticationService.GetSettingAsync("AutoCreateIncidents", "false")) == "true";
        MaxLogRetentionDays = int.TryParse(await _authenticationService.GetSettingAsync("MaxLogRetentionDays", "30"), out var d) ? d : 30;
        AlertEmail = await _authenticationService.GetSettingAsync("AlertEmail", "");
        AlertWebhookUrl = await _authenticationService.GetSettingAsync("AlertWebhookUrl", "");
        return Page();
    }

    public async Task<IActionResult> OnPostSaveSettingsAsync()
    {
        if (HttpContext.Session.GetString("IsAdmin") != "true")
        {
            return RedirectToPage("/Admin/Login");
        }

        await _authenticationService.SetSettingAsync("SiteName", SiteName ?? "Status");
        await _authenticationService.SetSettingAsync("ShowLogsPublicly", ShowLogsPublicly ? "true" : "false");
        await _authenticationService.SetSettingAsync("ShowUptimePublicly", ShowUptimePublicly ? "true" : "false");
        await _authenticationService.SetSettingAsync("ShowIncidentsPublicly", ShowIncidentsPublicly ? "true" : "false");
        await _authenticationService.SetSettingAsync("AutoCreateIncidents", AutoCreateIncidents ? "true" : "false");
        await _authenticationService.SetSettingAsync("MaxLogRetentionDays", MaxLogRetentionDays.ToString());
        await _authenticationService.SetSettingAsync("AlertEmail", AlertEmail ?? "");
        await _authenticationService.SetSettingAsync("AlertWebhookUrl", AlertWebhookUrl ?? "");
        Message = "Settings saved.";
        return await OnGetAsync();
    }

    public async Task<IActionResult> OnPostChangePasswordAsync()
    {
        if (HttpContext.Session.GetString("IsAdmin") != "true")
        {
            return RedirectToPage("/Admin/Login");
        }

        if (NewPassword != ConfirmPassword) { PasswordMessage = "error:Passwords do not match."; return await OnGetAsync(); }
        if (NewPassword.Length < 6) { PasswordMessage = "error:Password must be at least 6 characters."; return await OnGetAsync(); }
        if (await _authenticationService.ChangePasswordAsync(CurrentPassword, NewPassword))
        {
            PasswordMessage = "success:Password changed successfully.";
        }
        else
        {
            PasswordMessage = "error:Current password is incorrect.";
        }

        return await OnGetAsync();
    }
}
