package org.kar.karusic.api;

import java.io.InputStream;
import java.util.List;

import org.bson.types.ObjectId;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.kar.archidata.annotation.AsyncType;
import org.kar.archidata.annotation.FormDataOptional;
import org.kar.archidata.annotation.TypeScriptProgress;
import org.kar.archidata.dataAccess.DBAccess;
import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.dataAccess.addOnSQL.AddOnDataJson;
import org.kar.archidata.dataAccess.options.CheckFunction;
import org.kar.archidata.tools.DataTools;
import org.kar.karusic.model.Artist;
import org.kar.karusic.model.Artist.ArtistChecker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.annotation.security.RolesAllowed;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.PATCH;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/artist")
@Produces({ MediaType.APPLICATION_JSON })
public class ArtistResource {
	private static final Logger LOGGER = LoggerFactory.getLogger(ArtistResource.class);
	static final ArtistChecker CHECKER = new ArtistChecker();

	@GET
	@Path("{id}")
	@RolesAllowed("USER")
	public Artist get(@PathParam("id") final Long id) throws Exception {
		return DataAccess.get(Artist.class, id);
	}

	@GET
	@RolesAllowed("USER")
	public List<Artist> gets() throws Exception {
		return DataAccess.gets(Artist.class);
	}

	@POST
	@RolesAllowed("ADMIN")
	@Consumes(MediaType.APPLICATION_JSON)
	public Artist post(final Artist data) throws Exception {
		return DataAccess.insert(data, new CheckFunction(CHECKER));
	}

	@PATCH
	@Path("{id}")
	@RolesAllowed("ADMIN")
	@Consumes(MediaType.APPLICATION_JSON)
	public Artist patch(@PathParam("id") final Long id, @AsyncType(Artist.class) final String jsonRequest) throws Exception {
		DataAccess.updateWithJson(Artist.class, id, jsonRequest, new CheckFunction(CHECKER));
		return DataAccess.get(Artist.class, id);
	}

	@DELETE
	@Path("{id}")
	@RolesAllowed("ADMIN")
	public void remove(@PathParam("id") final Long id) throws Exception {
		DataAccess.delete(Artist.class, id);
	}

	@POST
	@Path("{id}/cover")
	@RolesAllowed("ADMIN")
	@Consumes({ MediaType.MULTIPART_FORM_DATA })
	@TypeScriptProgress
	public Artist uploadCover(@PathParam("id") final Long id, @FormDataOptional @FormDataParam("uri") final String uri, @FormDataOptional @FormDataParam("file") final InputStream fileInputStream,
			@FormDataOptional @FormDataParam("file") final FormDataContentDisposition fileMetaData) throws Exception {
		try (DBAccess db = DBAccess.createInterface()) {
			if (uri != null) {
				DataTools.uploadCoverFromUri(db, Artist.class, id, uri);
			} else {
				DataTools.uploadCover(db, Artist.class, id, fileInputStream, fileMetaData);
			}
			return db.get(Artist.class, id);
		}
	}

	@DELETE
	@Path("{id}/cover/{coverId}")
	@RolesAllowed("ADMIN")
	public Artist removeCover(@PathParam("id") final Long id, @PathParam("coverId") final ObjectId coverId) throws Exception {
		try (DBAccess db = DBAccess.createInterface()) {
			AddOnDataJson.removeLink(db, Artist.class, "id", id, "covers", coverId);
			return db.get(Artist.class, id);
		}
	}
}
