module IHP.IDE.CodeGen.Types where

import IHP.Prelude
import IHP.IDE.SchemaDesigner.Types

data GeneratorAction
    = CreateFile { GeneratorAction -> Text
filePath :: Text, GeneratorAction -> Text
fileContent :: Text }
    | AppendToFile { filePath :: Text, fileContent :: Text }
    | AppendToMarker { GeneratorAction -> Text
marker :: Text, filePath :: Text, fileContent :: Text }
    | AddImport { filePath :: Text, fileContent :: Text }
    | AddAction { filePath :: Text, fileContent :: Text }
    | AddToDataConstructor { GeneratorAction -> Text
dataConstructor :: Text, filePath :: Text, fileContent :: Text }
    | AddMountToFrontController { filePath :: Text, GeneratorAction -> Text
applicationName :: Text }
    | EnsureDirectory { GeneratorAction -> Text
directory :: Text }
    | RunShellCommand { GeneratorAction -> Text
shellCommand :: Text }
    deriving (Int -> GeneratorAction -> ShowS
[GeneratorAction] -> ShowS
GeneratorAction -> String
(Int -> GeneratorAction -> ShowS)
-> (GeneratorAction -> String)
-> ([GeneratorAction] -> ShowS)
-> Show GeneratorAction
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GeneratorAction -> ShowS
showsPrec :: Int -> GeneratorAction -> ShowS
$cshow :: GeneratorAction -> String
show :: GeneratorAction -> String
$cshowList :: [GeneratorAction] -> ShowS
showList :: [GeneratorAction] -> ShowS
Show, GeneratorAction -> GeneratorAction -> Bool
(GeneratorAction -> GeneratorAction -> Bool)
-> (GeneratorAction -> GeneratorAction -> Bool)
-> Eq GeneratorAction
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GeneratorAction -> GeneratorAction -> Bool
== :: GeneratorAction -> GeneratorAction -> Bool
$c/= :: GeneratorAction -> GeneratorAction -> Bool
/= :: GeneratorAction -> GeneratorAction -> Bool
Eq)



fieldsForTable :: [Statement] -> Text -> Maybe [Text]
fieldsForTable :: [Statement] -> Text -> Maybe [Text]
fieldsForTable [Statement]
database Text
name =
    case [Statement] -> Text -> Maybe Statement
getTable [Statement]
database Text
name of
        Just (StatementCreateTable CreateTable { [Column]
columns :: [Column]
$sel:columns:CreateTable :: CreateTable -> [Column]
columns, PrimaryKeyConstraint
primaryKeyConstraint :: PrimaryKeyConstraint
$sel:primaryKeyConstraint:CreateTable :: CreateTable -> PrimaryKeyConstraint
primaryKeyConstraint }) -> [Column]
columns
                [Column] -> ([Column] -> [Column]) -> [Column]
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> (Column -> Bool) -> [Column] -> [Column]
forall a. (a -> Bool) -> [a] -> [a]
filter (PrimaryKeyConstraint -> Column -> Bool
columnRelevantForCreateOrEdit PrimaryKeyConstraint
primaryKeyConstraint)
                [Column] -> ([Column] -> [Text]) -> [Text]
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> (Column -> Text) -> [Column] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (.name)
                [Text] -> ([Text] -> [Text]) -> [Text]
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
columnNameToFieldName
                [Text] -> ([Text] -> Maybe [Text]) -> Maybe [Text]
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> [Text] -> Maybe [Text]
forall a. a -> Maybe a
Just
        Maybe Statement
_ -> Maybe [Text]
forall a. Maybe a
Nothing
-- | Returns True when a column should be part of the generated controller or forms
--
-- Returrns @False@ for primary keys, or fields such as @created_at@
columnRelevantForCreateOrEdit :: PrimaryKeyConstraint -> Column -> Bool
columnRelevantForCreateOrEdit :: PrimaryKeyConstraint -> Column -> Bool
columnRelevantForCreateOrEdit PrimaryKeyConstraint
_ Column
column
    | (Column
column.columnType PostgresType -> PostgresType -> Bool
forall a. Eq a => a -> a -> Bool
== PostgresType
PTimestamp Bool -> Bool -> Bool
|| Column
column.columnType PostgresType -> PostgresType -> Bool
forall a. Eq a => a -> a -> Bool
== PostgresType
PTimestampWithTimezone)
    Bool -> Bool -> Bool
&& (Maybe Expression -> Bool
forall a. Maybe a -> Bool
isJust (Column
column.defaultValue))
    = Bool
False
columnRelevantForCreateOrEdit (PrimaryKeyConstraint [Text]
primaryKeyColumns) Column
column =
    Column
column.name Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Text]
primaryKeyColumns

getTable :: [Statement] -> Text -> Maybe Statement
getTable :: [Statement] -> Text -> Maybe Statement
getTable [Statement]
schema Text
name = (Statement -> Bool) -> [Statement] -> Maybe Statement
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find Statement -> Bool
isTable [Statement]
schema
    where
        isTable :: Statement -> Bool
        isTable :: Statement -> Bool
isTable table :: Statement
table@(StatementCreateTable CreateTable { $sel:name:CreateTable :: CreateTable -> Text
name = Text
name' }) | Text
name Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
name' = Bool
True
        isTable Statement
_ = Bool
False