module Unison.Server.Local.Endpoints.Projects.Types
  ( ProjectListing (..),
    ProjectBranchListing (..),
  )
where

import Data.Aeson (ToJSON (..))
import Data.Aeson qualified as Aeson
import Data.OpenApi
import GHC.Generics ()
import Servant.Docs
import Unison.Core.Project (ProjectBranchName (UnsafeProjectBranchName), ProjectName (UnsafeProjectName))
import Unison.Prelude
import Unison.Server.Orphans ()
import Unison.Sqlite (FromRow (..), field)

data ProjectListing = ProjectListing
  { ProjectListing -> ProjectName
projectName :: ProjectName,
    ProjectListing -> Maybe ProjectBranchName
mostRecentActiveBranch :: Maybe ProjectBranchName
  }
  deriving stock (Int -> ProjectListing -> ShowS
[ProjectListing] -> ShowS
ProjectListing -> String
(Int -> ProjectListing -> ShowS)
-> (ProjectListing -> String)
-> ([ProjectListing] -> ShowS)
-> Show ProjectListing
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ProjectListing -> ShowS
showsPrec :: Int -> ProjectListing -> ShowS
$cshow :: ProjectListing -> String
show :: ProjectListing -> String
$cshowList :: [ProjectListing] -> ShowS
showList :: [ProjectListing] -> ShowS
Show, (forall x. ProjectListing -> Rep ProjectListing x)
-> (forall x. Rep ProjectListing x -> ProjectListing)
-> Generic ProjectListing
forall x. Rep ProjectListing x -> ProjectListing
forall x. ProjectListing -> Rep ProjectListing x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ProjectListing -> Rep ProjectListing x
from :: forall x. ProjectListing -> Rep ProjectListing x
$cto :: forall x. Rep ProjectListing x -> ProjectListing
to :: forall x. Rep ProjectListing x -> ProjectListing
Generic)

instance FromRow ProjectListing where
  fromRow :: RowParser ProjectListing
fromRow = ProjectName -> Maybe ProjectBranchName -> ProjectListing
ProjectListing (ProjectName -> Maybe ProjectBranchName -> ProjectListing)
-> RowParser ProjectName
-> RowParser (Maybe ProjectBranchName -> ProjectListing)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RowParser ProjectName
forall a. FromField a => RowParser a
field RowParser (Maybe ProjectBranchName -> ProjectListing)
-> RowParser (Maybe ProjectBranchName) -> RowParser ProjectListing
forall a b. RowParser (a -> b) -> RowParser a -> RowParser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> RowParser (Maybe ProjectBranchName)
forall a. FromField a => RowParser a
field

instance ToSchema ProjectListing

instance ToJSON ProjectListing where
  toJSON :: ProjectListing -> Value
toJSON (ProjectListing ProjectName
projectName Maybe ProjectBranchName
mostRecentActiveBranch) =
    [Pair] -> Value
Aeson.object
      [ Key
"projectName" Key -> ProjectName -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
Aeson..= ProjectName
projectName,
        Key
"activeBranchRef" Key -> Maybe ProjectBranchName -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
Aeson..= Maybe ProjectBranchName
mostRecentActiveBranch
      ]

instance ToSample ProjectListing where
  toSamples :: Proxy ProjectListing -> [(Text, ProjectListing)]
toSamples Proxy ProjectListing
_ =
    ProjectListing -> [(Text, ProjectListing)]
forall a. a -> [(Text, a)]
singleSample (ProjectListing -> [(Text, ProjectListing)])
-> ProjectListing -> [(Text, ProjectListing)]
forall a b. (a -> b) -> a -> b
$ ProjectName -> Maybe ProjectBranchName -> ProjectListing
ProjectListing (Text -> ProjectName
UnsafeProjectName Text
"my-project") Maybe ProjectBranchName
forall a. Maybe a
Nothing

data ProjectBranchListing = ProjectBranchListing
  { ProjectBranchListing -> ProjectBranchName
branchName :: ProjectBranchName
  }
  deriving stock (Int -> ProjectBranchListing -> ShowS
[ProjectBranchListing] -> ShowS
ProjectBranchListing -> String
(Int -> ProjectBranchListing -> ShowS)
-> (ProjectBranchListing -> String)
-> ([ProjectBranchListing] -> ShowS)
-> Show ProjectBranchListing
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ProjectBranchListing -> ShowS
showsPrec :: Int -> ProjectBranchListing -> ShowS
$cshow :: ProjectBranchListing -> String
show :: ProjectBranchListing -> String
$cshowList :: [ProjectBranchListing] -> ShowS
showList :: [ProjectBranchListing] -> ShowS
Show, (forall x. ProjectBranchListing -> Rep ProjectBranchListing x)
-> (forall x. Rep ProjectBranchListing x -> ProjectBranchListing)
-> Generic ProjectBranchListing
forall x. Rep ProjectBranchListing x -> ProjectBranchListing
forall x. ProjectBranchListing -> Rep ProjectBranchListing x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ProjectBranchListing -> Rep ProjectBranchListing x
from :: forall x. ProjectBranchListing -> Rep ProjectBranchListing x
$cto :: forall x. Rep ProjectBranchListing x -> ProjectBranchListing
to :: forall x. Rep ProjectBranchListing x -> ProjectBranchListing
Generic)

instance FromRow ProjectBranchListing where
  fromRow :: RowParser ProjectBranchListing
fromRow = ProjectBranchName -> ProjectBranchListing
ProjectBranchListing (ProjectBranchName -> ProjectBranchListing)
-> RowParser ProjectBranchName -> RowParser ProjectBranchListing
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RowParser ProjectBranchName
forall a. FromField a => RowParser a
field

instance ToSchema ProjectBranchListing

instance ToJSON ProjectBranchListing where
  toJSON :: ProjectBranchListing -> Value
toJSON ProjectBranchListing {ProjectBranchName
$sel:branchName:ProjectBranchListing :: ProjectBranchListing -> ProjectBranchName
branchName :: ProjectBranchName
branchName} =
    [Pair] -> Value
Aeson.object [Key
"branchName" Key -> ProjectBranchName -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
Aeson..= ProjectBranchName
branchName]

instance ToSample ProjectBranchListing where
  toSamples :: Proxy ProjectBranchListing -> [(Text, ProjectBranchListing)]
toSamples Proxy ProjectBranchListing
_ =
    ProjectBranchListing -> [(Text, ProjectBranchListing)]
forall a. a -> [(Text, a)]
singleSample (ProjectBranchListing -> [(Text, ProjectBranchListing)])
-> ProjectBranchListing -> [(Text, ProjectBranchListing)]
forall a b. (a -> b) -> a -> b
$ ProjectBranchName -> ProjectBranchListing
ProjectBranchListing (Text -> ProjectBranchName
UnsafeProjectBranchName Text
"my-branch")