import { IpcClientService } from "@autoplex/ipc";
import { Microservice } from "@autoplex/microservice";
import { SOCKET_PATH } from "./constants";
import { IMovie, IMovieDetails, IPaginatedResponse } from "./schema";

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

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

    /**
	 * Get the details of a movie
	 */
	public async movieDetails(tmdbId: number) {
		let result = await this.request("details", tmdbId);
		if (result.error) {
			console.error("Failed to fetch movie details:", result.error);
			throw new Error("Failed to fetch movie details");
		}
		return <IMovieDetails>result.data;
	}

	/**
	 * Find a movie by its IMDb ID
	 */
	public async findMovieFromImdb(imdbId: string) {
		let result = await this.request("find", imdbId);
		if (result.error) {
			console.error("Failed to find a movie by its IMDb ID:", result.error);
			throw new Error("Failed to find a movie by its IMDb ID");
		}
		return <IMovie|null>result.data;
	}

	/**
	 * Search for a movie
	 */
	public async searchMovie(query: string, year?: number, page?: number) {
		let result = await this.request("search", { query, year, page });
		if (result.error) {
			console.error("Failed to search for a movie:", result.error);
			throw new Error("Failed to search for a movie");
		}
		return <IPaginatedResponse<IMovie>>result.data;
	}

	/**
	 * Verify an IMDb ID
	 */
	public async verifyImdbId(imdbId: string) {
		let result = await this.request("verify_imdb_id", imdbId);
		if (result.error) {
			console.error("Failed to verify an IMDb ID:", result.error);
			throw new Error("Failed to verify an IMDb ID");
		}
		return <boolean>result.data;
	}
}
