@page "/article/{id:guid}/delete"
@using Microsoft.EntityFrameworkCore
@using Wave.Data
@using Wave.Utilities

@inject IDbContextFactory<ApplicationDbContext> ContextFactory
@inject NavigationManager Navigation
@inject ILogger<ArticleDeleteConfirm> Logger
@inject IStringLocalizer<ArticleView> Localizer

<PageTitle>@((Article is not null ? Localizer["Delete_Title"] : Localizer["NotFound_Title"]) + TitlePostfix)</PageTitle>

@if (Article is not null) {
	<BoardComponent CenterContent="true">
		<BoardCardComponent Heading="@Localizer["Delete_Title"]">
			<Alert CanRemove="false" Type="Alert.MessageType.Warning">
				<strong>@Localizer["Delete_Warning"]</strong>
			</Alert>
			<p class="my-3">@Article.Title</p>
			<form @formname="delete" method="post" @onsubmit="Delete">
				<AntiforgeryToken />
				<button type="submit" class="btn btn-error w-full">
					@Localizer["Delete_Confirm"]
				</button>
			</form>
		</BoardCardComponent>
	</BoardComponent>
} else {
	<h1 class="text-3xl lg:text-5xl font-light mb-6 text-primary">@Localizer["NotFound_Title"]</h1>
	<p class="my-3">@Localizer["NotFound_Description"]</p>
	<a class="btn btn-primary" href="/">@Localizer["NotFound_BackToHome_Label"]</a>
}


@code {
	[CascadingParameter(Name = "TitlePostfix")]
	private string TitlePostfix { get; set; } = default!;
	[CascadingParameter]
	public HttpContext HttpContext { get; set; } = default!;

	[Parameter]
	public Guid Id { get; set; }

	private Article? Article { get; set; }

	protected override async Task OnInitializedAsync() {
		await using var context = await ContextFactory.CreateDbContextAsync();

		var article = await context.Set<Article>().IgnoreQueryFilters()
			.Include(a => a.Author).Include(a => a.Reviewer)
			.Where(a => !a.IsDeleted).FirstOrDefaultAsync(a => a.Id == Id);
		if (article.AllowedToDelete(HttpContext.User)) Article = article;
	}

	private async Task Delete() {
		if (!Article.AllowedToDelete(HttpContext.User)) return;

		var context = await ContextFactory.CreateDbContextAsync();
		Article!.IsDeleted = true;
		context.Entry(Article).State = EntityState.Modified;
		await context.SaveChangesAsync();

		Navigation.NavigateTo("/");
	}
}
