import { IMovie } from "@autoplex-api/search";
import { IpcClientService } from "@autoplex/ipc";
import { Microservice } from "@autoplex/microservice";
import { SOCKET_PATH } from "./constants";

/**
 * Internal methods
 */
export const enum IpcMethod {
    // Movie Requests
    ActiveMovieRequests        = "movie_requests_active",
    ActiveMovieRequestsForUser = "movie_requests_active_for_user",
    MapMovieRequests           = "movie_requests_map",
    MovieRequestStatus         = "movie_requests_status",
    CreateMovieRequest         = "movie_request_create",
    CancelMovieRequest         = "movie_request_cancel"
}

export class IpcClient<M extends Microservice = Microservice> extends IpcClientService<M>
{
    /**
     * The service name
     */
    public readonly NAME = "Manager";

    /**
     * The path to the socket file
     */
    protected readonly SOCKET_PATH = SOCKET_PATH;

    // Movie Requests ------------------------------------------------------------------------------

    /**
     * Fetch all active movie requests
     */
    public async allActiveMovieRequests() {
        return await this.request(IpcMethod.ActiveMovieRequests);
    }

    /**
     * Fetch active movie requests for the particular user
     */
    public async activeMovieRequestsForUser(userId: number) {
        return await this.request(IpcMethod.ActiveMovieRequestsForUser, userId);
    }

    /**
     * Find movie requests for the given movie list if they exist
     */
    public async mapActiveMovieRequests(movies: IMovie[]) {
        return await this.request(IpcMethod.MapMovieRequests, movies);
    }

    /**
     * Get the status of the given movie requests
     */
    public async movieRequestStatus(ticketIds: number[]) {
        return await this.request(IpcMethod.MovieRequestStatus, ticketIds);
    }

    /**
     * Create a movie request ticket
     */
    public async createMovieRequest(userId: number, tmdbId: number) {
        return await this.request<number>(IpcMethod.CreateMovieRequest, {
            userId, tmdbId
        });
    }

    /**
     * Cancel a movie request ticket
     */
    public async cancelMovieRequest(ticketId: number) {
        return await this.request(IpcMethod.CancelMovieRequest, ticketId);
    }
}
