{-# language DataKinds #-}
{-# language FlexibleContexts #-}
{-# language FlexibleInstances #-}
{-# language MultiParamTypeClasses #-}
{-# language NamedFieldPuns #-}
{-# language ScopedTypeVariables #-}
{-# language TypeApplications #-}
{-# language TypeFamilies #-}
{-# language UndecidableInstances #-}
{-# language ViewPatterns #-}

module Rel8.Table.Name
  ( namesFromLabels
  , namesFromLabelsWith
  , showLabels
  , showNames
  )
where

-- base
import Data.Foldable ( fold )
import Data.Functor.Const ( Const( Const ), getConst )
import Data.List.NonEmpty ( NonEmpty, intersperse, nonEmpty )
import Data.Maybe ( fromMaybe )
import Prelude

-- rel8
import Rel8.Schema.HTable ( htabulate, htabulateA, hfield, hspecs )
import Rel8.Schema.Name ( Name( Name ) )
import Rel8.Schema.Spec ( Spec(..) )
import Rel8.Table ( Table(..) )


-- | Construct a table in the 'Name' context containing the names of all
-- columns. Nested column names will be combined with @/@.
--
-- See also: 'namesFromLabelsWith'.
namesFromLabels :: Table Name a => a
namesFromLabels :: forall a. Table Name a => a
namesFromLabels = forall a. Table Name a => (NonEmpty String -> String) -> a
namesFromLabelsWith NonEmpty String -> String
go
  where
    go :: NonEmpty String -> String
go = forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> NonEmpty a -> NonEmpty a
intersperse String
"/"


-- | Construct a table in the 'Name' context containing the names of all
-- columns. The supplied function can be used to transform column names.
--
-- This function can be used to generically derive the columns for a
-- 'TableSchema'. For example,
--
-- @
-- myTableSchema :: TableSchema (MyTable Name)
-- myTableSchema = TableSchema
--   { columns = namesFromLabelsWith last
--   }
-- @
--
-- will construct a 'TableSchema' where each columns names exactly corresponds
-- to the name of the Haskell field.
namesFromLabelsWith :: Table Name a
  => (NonEmpty String -> String) -> a
namesFromLabelsWith :: forall a. Table Name a => (NonEmpty String -> String) -> a
namesFromLabelsWith NonEmpty String -> String
f = forall (context :: * -> *) a.
Table context a =>
Columns a context -> a
fromColumns forall a b. (a -> b) -> a -> b
$ forall (t :: HTable) (context :: * -> *).
HTable t =>
(forall a. HField t a -> context a) -> t context
htabulate forall a b. (a -> b) -> a -> b
$ \HField (Columns a) a
field ->
  case forall (t :: HTable) (context :: * -> *) a.
HTable t =>
t context -> HField t a -> context a
hfield forall (t :: HTable). HTable t => t Spec
hspecs HField (Columns a) a
field of
    Spec {[String]
labels :: forall a. Spec a -> [String]
labels :: [String]
labels} -> forall a. String -> Name a
Name (NonEmpty String -> String
f ([String] -> NonEmpty String
renderLabels [String]
labels))


showLabels :: forall a. Table (Context a) a => a -> [NonEmpty String]
showLabels :: forall a. Table (Context a) a => a -> [NonEmpty String]
showLabels a
_ = forall {k} a (b :: k). Const a b -> a
getConst forall a b. (a -> b) -> a -> b
$
  forall (t :: HTable) (m :: * -> *) (context :: * -> *).
(HTable t, Apply m) =>
(forall a. HField t a -> m (context a)) -> m (t context)
htabulateA @(Columns a) forall a b. (a -> b) -> a -> b
$ \HField (Columns a) a
field -> case forall (t :: HTable) (context :: * -> *) a.
HTable t =>
t context -> HField t a -> context a
hfield forall (t :: HTable). HTable t => t Spec
hspecs HField (Columns a) a
field of
    Spec {[String]
labels :: [String]
labels :: forall a. Spec a -> [String]
labels} -> forall {k} a (b :: k). a -> Const a b
Const (forall (f :: * -> *) a. Applicative f => a -> f a
pure ([String] -> NonEmpty String
renderLabels [String]
labels))


showNames :: forall a. Table Name a => a -> NonEmpty String
showNames :: forall a. Table Name a => a -> NonEmpty String
showNames (forall (context :: * -> *) a.
Table context a =>
a -> Columns a context
toColumns -> Columns a Name
names) = forall {k} a (b :: k). Const a b -> a
getConst forall a b. (a -> b) -> a -> b
$
  forall (t :: HTable) (m :: * -> *) (context :: * -> *).
(HTable t, Apply m) =>
(forall a. HField t a -> m (context a)) -> m (t context)
htabulateA @(Columns a) forall a b. (a -> b) -> a -> b
$ \HField (Columns a) a
field -> case forall (t :: HTable) (context :: * -> *) a.
HTable t =>
t context -> HField t a -> context a
hfield Columns a Name
names HField (Columns a) a
field of
    Name String
name -> forall {k} a (b :: k). a -> Const a b
Const (forall (f :: * -> *) a. Applicative f => a -> f a
pure String
name)


renderLabels :: [String] -> NonEmpty String
renderLabels :: [String] -> NonEmpty String
renderLabels [String]
labels = forall a. a -> Maybe a -> a
fromMaybe (forall (f :: * -> *) a. Applicative f => a -> f a
pure String
"anon") (forall a. [a] -> Maybe (NonEmpty a)
nonEmpty [String]
labels )