// <copyright file="PostgresVersionMismatchException.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>

namespace Jellyfin.Database.Providers.Postgres.Exceptions;

using System;

/// <summary>
/// Exception thrown when there is a version mismatch between PostgreSQL server and client tools (pg_dump/pg_restore).
/// </summary>
public class PostgresVersionMismatchException : Exception
{
    /// <summary>
    /// Initializes a new instance of the <see cref="PostgresVersionMismatchException"/> class.
    /// </summary>
    /// <param name="serverVersion">The PostgreSQL server version.</param>
    /// <param name="clientVersion">The pg_dump/pg_restore client version.</param>
    public PostgresVersionMismatchException(string serverVersion, string clientVersion)
        : base(BuildMessage(serverVersion, clientVersion))
    {
        ServerVersion = serverVersion;
        ClientVersion = clientVersion;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="PostgresVersionMismatchException"/> class.
    /// </summary>
    /// <param name="serverVersion">The PostgreSQL server version.</param>
    /// <param name="clientVersion">The pg_dump/pg_restore client version.</param>
    /// <param name="innerException">The inner exception.</param>
    public PostgresVersionMismatchException(string serverVersion, string clientVersion, Exception innerException)
        : base(BuildMessage(serverVersion, clientVersion), innerException)
    {
        ServerVersion = serverVersion;
        ClientVersion = clientVersion;
    }

    /// <summary>
    /// Gets the PostgreSQL server version.
    /// </summary>
    public string ServerVersion { get; }

    /// <summary>
    /// Gets the pg_dump/pg_restore client version.
    /// </summary>
    public string ClientVersion { get; }

    private static string BuildMessage(string serverVersion, string clientVersion)
    {
        return $"PostgreSQL server version ({serverVersion}) does not match pg_dump version ({clientVersion}). " +
               "Please upgrade your PostgreSQL client tools to match or exceed the server version. " +
               "You can specify a custom path to pg_dump in the database backup options configuration.";
    }
}
