package pgdump

import (
	"context"
	"fmt"
	"io"
)

// Options holds configuration for the pg_dump invocation.
type Options struct {
	Host          string
	Port          int
	Database      string
	User          string
	Password      string
	Key           string
	ExcludeTables []string
}

// Dumper defines the contract for executing pg_dump.
type Dumper interface {
	Dump(
		ctx context.Context,
		opts Options,
		sink io.Writer,
	) error
}

// pgDumpFailedError is returned when pg_dump exits with a non-zero status.
type pgDumpFailedError struct {
	exitCode int
}

func (e *pgDumpFailedError) Error() string {
	return fmt.Sprintf("pg_dump failed with exit code %d", e.exitCode)
}

// Is reports whether target is a pgDumpFailedError with the same exit code.
func (e *pgDumpFailedError) Is(target error) bool {
	other, ok := target.(*pgDumpFailedError)
	if !ok {
		return false
	}
	return e.exitCode == other.exitCode
}

// ErrPgDumpFailed creates a new pg_dump failed error with the given exit code.
func ErrPgDumpFailed(exitCode int) error {
	return &pgDumpFailedError{exitCode: exitCode}
}
