module IHP.IDE.SchemaDesigner.View.Migrations.New where
import IHP.ViewPrelude
import IHP.IDE.ToolServer.Types
import IHP.IDE.ToolServer.Routes ()
import IHP.IDE.CodeGen.Types

data NewView = NewView { NewView -> [GeneratorAction]
plan :: [GeneratorAction] }

instance View NewView where
    html :: (?context::ControllerContext, ?view::NewView) => NewView -> Html
html NewView { [GeneratorAction]
$sel:plan:NewView :: NewView -> [GeneratorAction]
plan :: [GeneratorAction]
.. } = [hsx|
        <div class="container pt-5">
            <h1>New Migration</h1>
            <div class="migration pending">
                {renderForm sqlStatements}
            </div>
        </div>
    |]
        where
            sqlStatements :: Text
sqlStatements = [GeneratorAction]
plan
                [GeneratorAction] -> ([GeneratorAction] -> [Text]) -> [Text]
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> (GeneratorAction -> Maybe Text) -> [GeneratorAction] -> [Text]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe \case
                    CreateFile { Text
fileContent :: Text
$sel:fileContent:CreateFile :: GeneratorAction -> Text
fileContent } -> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
fileContent
                    GeneratorAction
_ -> Maybe Text
forall a. Maybe a
Nothing
                [Text] -> ([Text] -> Maybe Text) -> Maybe Text
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> [Text] -> Maybe Text
forall a. [a] -> Maybe a
headMay
                Maybe Text -> (Maybe Text -> Text) -> Text
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
""


renderForm :: Text -> Html
renderForm :: Text -> Html
renderForm Text
sqlStatements = [hsx|
<form method="POST" action={CreateMigrationAction}>
    <div class="form-group" id="form-group-migration_sqlStatements">
        <textarea name="sqlStatements" placeholder="" id="migration_sqlStatements" class="form-control">{sqlStatements}</textarea>
    </div>
    <div id="migration_sqlStatements_ace"/>

    <div class="d-flex">
        <button class="btn btn-primary">
            <span id="addMigrationVerb">Run</span> Migration
        </button>

        <div class="ml-3 d-flex align-items-center">
            <div class="custom-control custom-switch d-flex align-items-center">
                <input type="checkbox" class="custom-control-input" id="createOnlySwitch" name="createOnly" onchange="addMigrationVerb.innerText = this.checked ? 'Add' : 'Run'"/>
                <label class="custom-control-label text-muted" for="createOnlySwitch" style="font-size: 14px">Create only, don't run yet</label>
            </div>
        </div>
    </div>
</form>
|]