import type { UmbDataSource } from '@umbraco-cms/backoffice/repository';
import {
	CreateDocumentTypeRequestModel,
	DocumentTypeResource,
	DocumentTypeResponseModel,
	UpdateDocumentTypeRequestModel,
} from '@umbraco-cms/backoffice/backend-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
import { UmbId } from '@umbraco-cms/backoffice/id';

/**
 * A data source for the Document Type that fetches data from the server
 * @export
 * @class UmbDocumentTypeServerDataSource
 * @implements {RepositoryDetailDataSource}
 */
export class UmbDocumentTypeServerDataSource
	implements
		UmbDataSource<CreateDocumentTypeRequestModel, any, UpdateDocumentTypeRequestModel, DocumentTypeResponseModel>
{
	#host: UmbControllerHost;

	/**
	 * Creates an instance of UmbDocumentServerDataSource.
	 * @param {UmbControllerHost} host
	 * @memberof UmbDocumentServerDataSource
	 */
	constructor(host: UmbControllerHost) {
		this.#host = host;
	}

	/**
	 * Fetches a Document with the given id from the server
	 * @param {string} id
	 * @return {*}
	 * @memberof UmbDocumentTypeServerDataSource
	 */
	async read(id: string) {
		if (!id) {
			throw new Error('Id is missing');
		}

		return tryExecuteAndNotify(
			this.#host,
			DocumentTypeResource.getDocumentTypeById({
				id: id,
			}),
		);
	}

	/**
	 * Creates a new Document scaffold
	 * @param {(string | null)} parentId
	 * @return {*}
	 * @memberof UmbDocumentTypeServerDataSource
	 */
	async createScaffold(parentId: string | null) {
		//, parentId: string | null
		const data: DocumentTypeResponseModel = {
			id: UmbId.new(),
			//parentId: parentId,
			name: '',
			alias: '',
			description: '',
			icon: 'icon-document',
			allowedAsRoot: false,
			variesByCulture: false,
			variesBySegment: false,
			isElement: false,
			allowedContentTypes: [],
			compositions: [],
			allowedTemplateIds: [],
			defaultTemplateId: null,
			cleanup: {
				preventCleanup: false,
				keepAllVersionsNewerThanDays: null,
				keepLatestVersionPerDayForDays: null,
			},
			properties: [],
			containers: [],
		};

		return { data };
	}

	/**
	 * Inserts a new Document Type on the server
	 * @param {CreateDocumentTypeRequestModel} documentType
	 * @return {*}
	 * @memberof UmbDocumentTypeServerDataSource
	 */
	async create(documentType: CreateDocumentTypeRequestModel) {
		if (!documentType) throw new Error('Document Type is missing');
		return tryExecuteAndNotify(
			this.#host,
			DocumentTypeResource.postDocumentType({
				requestBody: documentType,
			}),
		);
	}

	/**
	 * Updates a Document Type on the server
	 * @param {string} id
	 * @param {Document} documentType
	 * @return {*}
	 * @memberof UmbDocumentTypeServerDataSource
	 */
	async update(id: string, documentType: UpdateDocumentTypeRequestModel) {
		if (!id) throw new Error('Id is missing');

		documentType = { ...documentType };

		// TODO: Hack to remove some props that ruins the document-type post end-point.
		(documentType as any).id = undefined;

		return tryExecuteAndNotify(this.#host, DocumentTypeResource.putDocumentTypeById({ id, requestBody: documentType }));
	}

	/**
	 * Deletes a Template on the server
	 * @param {string} id
	 * @return {*}
	 * @memberof UmbDocumentTypeServerDataSource
	 */
	async delete(id: string) {
		if (!id) {
			throw new Error('Id is missing');
		}

		// TODO: Hack the type to avoid type-error here:
		return tryExecuteAndNotify(this.#host, DocumentTypeResource.deleteDocumentTypeById({ id })) as any;
	}

	/**
	 * Get the allowed document types for a given parent id
	 * @param {string} id
	 * @return {*}
	 * @memberof UmbDocumentTypeServerDataSource
	 */
	async getAllowedChildrenOf(id: string) {
		if (!id) throw new Error('Id is missing');

		return tryExecuteAndNotify(
			this.#host,
			fetch(`/umbraco/management/api/v1/document-type/allowed-children-of/${id}`, {
				method: 'GET',
				headers: {
					'Content-Type': 'application/json',
				},
			}).then((res) => res.json()),
		);
	}
}
