{-# LANGUAGE QuasiQuotes #-}

module Restyler.Content
    ( commentBody
    , pullRequestDescription
    )
where

import Restyler.Prelude

import GitHub.Data (IssueNumber, unIssueNumber)
import Restyler.PullRequest
import Restyler.Restyler
import Restyler.RestylerResult
import Text.Shakespeare.Text (st)

-- brittany-disable-next-binding

commentBody
    :: IssueNumber -- ^ Restyled PR Number
    -> Text
commentBody :: IssueNumber -> Text
commentBody n :: IssueNumber
n = [st|
Hey there-

I'm a [bot][homepage], here to let you know that some code in this PR might not
match the team's automated styling. I ran the team's auto-reformatting tools on
the files changed in this PR and found some differences. Those differences can
be seen in ##{unIssueNumber n}.

Please see that Pull Request's description for more details.

[homepage]: https://restyled.io
|]

-- brittany-disable-next-binding

pullRequestDescription
    :: Maybe URL -- ^ Job URL, if we have it
    -> PullRequest -- ^ Original PR
    -> [RestylerResult]
    -> Text
pullRequestDescription :: Maybe URL -> PullRequest -> [RestylerResult] -> Text
pullRequestDescription mJobUrl :: Maybe URL
mJobUrl pullRequest :: PullRequest
pullRequest results :: [RestylerResult]
results
    | PullRequest -> Bool
pullRequestIsFork PullRequest
pullRequest = [st|
A duplicate of ##{n} with additional commits that automatically address
incorrect style, created by [Restyled][].

Since the original Pull Request was opened as a fork in a contributor's
repository, we are unable to create a Pull Request branching from it with only
the style fixes.

The following Restylers #{madeFixes}:

#{resultsList}

To incorporate these changes, you can either:

1. Merge this Pull Request *instead of* the original, or

1. Ask your contributor to locally incorporate these commits and push them to
   the original Pull Request

   <details>
       <summary>Expand for example instructions</summary>

       ```console
       git remote add upstream #{getUrl $ pullRequestCloneUrl pullRequest}
       git fetch upstream pull/<this PR number>/head
       git merge --ff-only FETCH_HEAD
       git push
       ```

   </details>

#{footer}
|]
    | Bool
otherwise = [st|
Automated style fixes for ##{n}, created by [Restyled][].

The following restylers #{madeFixes}:

#{resultsList}

To incorporate these changes, merge this Pull Request into the original. We
recommend using the Squash or Rebase strategies.

#{footer}
|]
  where
    -- This variable is just so that we can wrap our content above such that
    -- when the link is rendered at ~3 digits, it looks OK.
    n :: Int
n = IssueNumber -> Int
unIssueNumber (IssueNumber -> Int) -> IssueNumber -> Int
forall a b. (a -> b) -> a -> b
$ PullRequest -> IssueNumber
pullRequestNumber PullRequest
pullRequest

    -- Link the "made fixes" line to the Job log, if we can
    madeFixes :: Text
madeFixes = case Maybe URL
mJobUrl of
        Nothing -> "made fixes"
        Just jobUrl :: URL
jobUrl -> "[made fixes](" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> URL -> Text
getUrl URL
jobUrl Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ")"

    -- N.B. Assumes something committed changes, otherwise we'd not be opening
    -- this PR at all
    resultsList :: String
resultsList = [String] -> String
unlines
        ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (RestylerResult -> String) -> [RestylerResult] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (("- " String -> String -> String
forall a. Semigroup a => a -> a -> a
<>) (String -> String)
-> (RestylerResult -> String) -> RestylerResult -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Restyler -> String
rName (Restyler -> String)
-> (RestylerResult -> Restyler) -> RestylerResult -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RestylerResult -> Restyler
rrRestyler)
        ([RestylerResult] -> [String]) -> [RestylerResult] -> [String]
forall a b. (a -> b) -> a -> b
$ (RestylerResult -> Bool) -> [RestylerResult] -> [RestylerResult]
forall a. (a -> Bool) -> [a] -> [a]
filter RestylerResult -> Bool
restylerCommittedChanges [RestylerResult]
results

    footer :: Text
footer = [st|
**NOTE**: As work continues on the original Pull Request, this process will
re-run and update (force-push) this Pull Request with updated style fixes as
necessary. If the style is fixed manually at any point (i.e. this process finds
no fixes to make), this Pull Request will be closed automatically.

Sorry if this was unexpected. To disable it, see our [documentation][].

[restyled]: https://restyled.io
[documentation]: https://github.com/restyled-io/restyled.io/wiki/Disabling-Restyled
|]