import { HttpApiEndpoint } from '@effect/platform';
import { Description, Summary, Title } from '@effect/platform/OpenApi';
import { StudioCMSPageFolderStructure } from '@withstudiocms/sdk/tables';
import * as Schema from 'effect/Schema';
import { RestAPIError } from '../../errors.js';
import { RestAPIAuthorization } from '../../middleware.js';
import {
	DeletionSuccess,
	FolderBase,
	IdParamString,
	PublicV1FolderGetSearchParams,
	PublicV1FolderSelect,
	successResponseSchema,
} from '../../schemas.js';

/**
 * GET /folders
 * Retrieves a list of folders.
 */
export const FolderIndexGet = HttpApiEndpoint.get('getFolders', '/folders')
	.annotate(Title, 'Get Folders')
	.annotate(Summary, 'Retrieve Folders')
	.annotate(
		Description,
		'Retrieves a list of folders, with optional filtering by name and parent ID.'
	)
	.setUrlParams(PublicV1FolderGetSearchParams)
	.middleware(RestAPIAuthorization)
	.addSuccess(Schema.Array(PublicV1FolderSelect))
	.addError(RestAPIError, { status: 404 })
	.addError(RestAPIError, { status: 500 });

/**
 * POST /folders
 * Creates a new folder.
 */
export const FolderIndexPost = HttpApiEndpoint.post('createFolder', '/folders')
	.annotate(Title, 'Create Folder')
	.annotate(Summary, 'Create Folder')
	.annotate(Description, 'Creates a new folder.')
	.setPayload(StudioCMSPageFolderStructure.Insert.omit('id'))
	.middleware(RestAPIAuthorization)
	.addSuccess(successResponseSchema)
	.addError(RestAPIError, { status: 400 })
	.addError(RestAPIError, { status: 500 });

/**
 * GET /folders/{id}
 * Retrieves a folder by its ID.
 */
export const FolderByIdGet = HttpApiEndpoint.get('getFolder', '/folders/:id')
	.setPath(IdParamString)
	.annotate(Title, 'Get Folder by ID')
	.annotate(Summary, 'Retrieve Folder by ID')
	.annotate(Description, 'Retrieves a folder by its ID.')
	.middleware(RestAPIAuthorization)
	.addSuccess(PublicV1FolderSelect)
	.addError(RestAPIError, { status: 404 })
	.addError(RestAPIError, { status: 500 });

/**
 * PATCH /folders/{id}
 * Updates a folder by its ID.
 */
export const FolderByIdPatch = HttpApiEndpoint.patch('updateFolder', '/folders/:id')
	.setPath(IdParamString)
	.annotate(Title, 'Update Folder by ID')
	.annotate(Summary, 'Update Folder by ID')
	.annotate(Description, 'Updates a folder by its ID.')
	.setPayload(FolderBase)
	.middleware(RestAPIAuthorization)
	.addSuccess(successResponseSchema)
	.addError(RestAPIError, { status: 400 })
	.addError(RestAPIError, { status: 404 })
	.addError(RestAPIError, { status: 500 });

/**
 * DELETE /folders/{id}
 * Deletes a folder by its ID.
 */
export const FolderByIdDelete = HttpApiEndpoint.del('deleteFolder', '/folders/:id')
	.setPath(IdParamString)
	.annotate(Title, 'Delete Folder by ID')
	.annotate(Summary, 'Delete Folder by ID')
	.annotate(Description, 'Deletes a folder by its ID.')
	.middleware(RestAPIAuthorization)
	.addSuccess(DeletionSuccess)
	.addError(RestAPIError, { status: 404 })
	.addError(RestAPIError, { status: 500 });
