using Oap.OapApplication;
using OrchardCore.Autoroute.Models;
using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentFields.Settings;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.Data.Migration;
using OrchardCore.Flows.Models;
using OrchardCore.Html.Models;
using OrchardCore.Html.Settings;
using OrchardCore.Lists.Models;
using OrchardCore.Mvc.Utilities;
using OrchardCore.Title.Models;

namespace Oap.Content;

public class Migrations : DataMigration
{
	public Migrations(IContentDefinitionManager contentDefinitionManager)
	{
		m_contentDefinitionManager = contentDefinitionManager;
	}

	public async Task<int> CreateAsync()
	{
		await m_contentDefinitionManager
			.AlterTypeDefinitionAsync(ContentTypes.OapApplicationFormStep, type => type
				.Securable()
				.Draftable()
				.Versionable()
				.WithPart(nameof(TitlePart), part => part
					.WithPosition("1"))
				.WithPart(nameof(AutoroutePart), part => part
					.WithPosition("2")
					.WithSettings(new AutoroutePartSettings
					{
						AllowCustomPath = true,
						AllowUpdatePath = true,
					}))
				.WithPart(nameof(FlowPart), part => part
					.WithPosition("3")));

		await m_contentDefinitionManager
			.AlterTypeDefinitionAsync(ContentTypes.OapApplicationForm, type => type
				.Creatable()
				.Listable()
				.Securable()
				.Draftable()
				.Versionable()
				.WithPart(nameof(AutoroutePart), part => part
					.WithPosition("2")
					.WithSettings(new AutoroutePartSettings
					{
						AllowCustomPath = true,
						AllowUpdatePath = true,
					}))
				.WithPart(nameof(ListPart), part => part
					.WithDisplayName("Form Steps")
					.WithPosition("3")
					.WithSettings(new ListPartSettings
					{
						PageSize = 10,
						EnableOrdering = true,
						ContainedContentTypes = [ContentTypes.OapApplicationFormStep]
					})
				));

		await m_contentDefinitionManager
			.AlterPartDefinitionAsync(nameof(OapApplicationPart), builder => builder
				.Attachable()
				.WithField(nameof(OapApplicationPart.Status), field => field
					.OfType(nameof(TextField))
					.WithDisplayName(nameof(OapApplicationPart.Status))
					.WithDescription("The status of the OAP Application.")
					.WithEditor("PredefinedList")
					.WithSettings(new TextFieldPredefinedListEditorSettings
					{
						Options = new[]
						{
							new ListValueOption { Name = OapApplicationStatuses.InProgress, Value = OapApplicationStatuses.InProgress.HtmlClassify() },
							new ListValueOption { Name = OapApplicationStatuses.Submitted, Value = OapApplicationStatuses.Submitted.HtmlClassify() },
						},
						DefaultValue = OapApplicationStatuses.InProgress.HtmlClassify(),
						Editor = EditorOption.Radio,
					}))
				.WithDescription("Makes a content item into an OAP Application."));

		await m_contentDefinitionManager
			.AlterTypeDefinitionAsync(ContentTypes.OapApplication, type => type
				.Creatable()
				.Listable()
				.Securable()
				.WithPart(nameof(HtmlBodyPart), part => part
					.WithDisplayName("Notes")
					.WithSettings(new ContentTypePartSettings { Editor = "Wysiwyg", })
				)
				.WithPart(nameof(OapApplicationPart)));

		return 1;
	}

	public async Task<int> UpdateFrom1Async()
	{
		await m_contentDefinitionManager
			.AlterTypeDefinitionAsync(ContentTypes.OapApplicationFormStep, type => type
				.WithPart(nameof(HtmlBodyPart), part => part
					.WithPosition("2")
					.WithEditor("Wysiwyg")
					.WithSettings(new HtmlBodyPartSettings
					{
						SanitizeHtml = true,
					})));

		return 2;
	}

	private readonly IContentDefinitionManager m_contentDefinitionManager;
}
