<?php

namespace PublishPress\Future\Modules\Workflows\Controllers;

use PublishPress\Future\Framework\InitializableInterface;
use PublishPress\Future\Modules\Workflows\Models\WorkflowsModel;

class SampleWorkflows implements InitializableInterface
{
    public function __construct()
    {
    }

    public function initialize(): void
    {
        $this->checkSampleWorkflowsShouldBeCreated();
    }

    private function checkSampleWorkflowsShouldBeCreated(): void
    {
        $workflowsModel = new WorkflowsModel();

        if ($workflowsModel->hasCreatedSampleWorkflows()) {
            return;
        }

        $workflowsModel->createSampleWorkflows($this->getSamples());
    }

    private function getSamples(): array
    {
        return [
            [
                'title' => __('Notify when a post is updated', 'post-expirator'),
                'description' => __('Notify the site admin when a post is updated.', 'post-expirator'),
                'flow' => '{"nodes":[{"id":"n1717543440185","type":"trigger","position":{"x":0,"y":-140},"data":{"name":"trigger\/core.post-updated","elementaryType":"trigger","version":1,"slug":"onPostUpdated1","settings":{"postQuery":{"postSource":"custom","postType":[],"postId":[],"postStatus":[],"json":{"and":[{"==":[{"var":"{{onPostUpdated1.postAfter.type}}"},"post"]}]},"natural":"{{onPostUpdated1.postAfter.type}} is \'post\'"}}},"width":140,"height":68,"selected":true,"positionAbsolute":{"x":0,"y":-140},"dragging":false},{"id":"n1717543448599","type":"generic","position":{"x":0,"y":-20},"data":{"name":"action\/core.send-email","elementaryType":"action","version":1,"slug":"sendEmail1","settings":{"recipient":{"recipient":"global.site.admin_email","custom":""},"subject":"Post updated: {{onPostUpdated1.postBefore.title}}","message":"The post \"{{onPostUpdated1.postBefore.title}} was updated.\n\nThe new title is \"{{onPostUpdated1.postAfter.title}}\"."}},"width":140,"height":68,"selected":false,"positionAbsolute":{"x":0,"y":-20},"dragging":false}],"edges":[{"source":"n1717543440185","sourceHandle":"output","target":"n1717543448599","targetHandle":"input","type":"genericEdge","id":"n1717543440185-output-n1717543448599-input","markerEnd":{"type":"arrowclosed"}}],"viewport":{"x":360,"y":1079.5,"zoom":2},"editorVersion":"4.5.0"}', // phpcs:ignore Generic.Files.LineLength.TooLong
            ],
            [
                'title' => __('Unpublish to Draft after a week', 'post-expirator'),
                'description' => __('Change the post status to Draft a week after it was published.', 'post-expirator'), // phpcs:ignore Generic.Files.LineLength.TooLong
                'flow' => '{"nodes":[{"id":"n1717531507668","type":"trigger","position":{"x":12,"y":12},"data":{"name":"trigger\/core.save-post","elementaryType":"trigger","version":2,"slug":"onSavePost1","settings":{"postQuery":{"postSource":"custom","postType":["post"],"postId":[],"postStatus":["publish"],"json":{"and":[{"==":[{"var":"{{onSavePost1.post.type}}"},"post"]},{"==":[{"var":"{{onSavePost1.post.status}}"},"publish"]}]},"natural":"{{onSavePost1.post.type}} is \'post\', and {{onSavePost1.post.status}} is \'publish\'"}}},"width":140,"height":68,"selected":true,"targetPosition":"top","sourcePosition":"bottom","$H":284,"x":12,"y":12,"positionAbsolute":{"x":12,"y":12},"dragging":false},{"id":"n1717531530334","type":"generic","position":{"x":12,"y":252},"data":{"name":"action\/core.post-change-status","elementaryType":"action","version":1,"slug":"changePostStatus1","settings":{"newStatus":{"newStatus":"publish","status":"draft"},"post":{"variable":"{{onSavePost1.post}}"}}},"width":140,"height":68,"selected":false,"positionAbsolute":{"x":12,"y":252},"dragging":false,"targetPosition":"top","sourcePosition":"bottom","$H":333,"x":12,"y":252},{"id":"n1717531533130","type":"generic","position":{"x":12,"y":132},"data":{"name":"advanced\/core.schedule","elementaryType":"advanced","version":1,"slug":"schedule1","settings":{"schedule":{"whenToRun":"offset","dateSource":"onSavePost1.post.post_date","recurrence":"single","repeatUntil":"forever","repeatInterval":"3600","repeatTimes":"5","repeatUntilDate":"2024-06-11T20:06:02.100Z","unique":true,"priority":"10","specificDate":"2024-06-07T20:06:02.100Z","dateOffset":"+7 days"}}},"width":140,"height":68,"selected":false,"positionAbsolute":{"x":12,"y":132},"dragging":false,"targetPosition":"top","sourcePosition":"bottom","$H":335,"x":12,"y":132},{"id":"n1717531536270","type":"generic","position":{"x":12,"y":372},"data":{"name":"action\/core.send-email","elementaryType":"action","version":1,"slug":"sendEmail1","settings":{"recipient":{"recipient":"global.site.admin_email","custom":""}}},"width":140,"height":68,"targetPosition":"top","sourcePosition":"bottom","$H":337,"x":12,"y":372,"selected":false,"dragging":false,"positionAbsolute":{"x":12,"y":372}}],"edges":[{"source":"n1717531507668","sourceHandle":"output","target":"n1717531533130","targetHandle":"input","type":"genericEdge","id":"n1717531507668-output-n1717531533130-input","markerEnd":{"type":"arrowclosed"}},{"source":"n1717531533130","sourceHandle":"output","target":"n1717531530334","targetHandle":"input","type":"genericEdge","id":"n1717531533130-output-n1717531530334-input","markerEnd":{"type":"arrowclosed"}},{"source":"n1717531530334","sourceHandle":"output","target":"n1717531536270","targetHandle":"input","type":"genericEdge","id":"n1717531530334-output-n1717531536270-input","markerEnd":{"type":"arrowclosed"}}],"viewport":{"x":416,"y":535.5,"zoom":2},"editorVersion":"4.5.0-beta.2"}', // phpcs:ignore Generic.Files.LineLength.TooLong
            ],
            [
                'title' => __('Unpublish and change category after one week', 'post-expirator'),
                'description' => __('Move the post to draft and add a category after 15 days.', 'post-expirator'), // phpcs:ignore Generic.Files.LineLength.TooLong
                'flow' => '{"nodes":[{"id":"n1717542854679","type":"trigger","position":{"x":20,"y":-100},"data":{"name":"trigger\/core.save-post","elementaryType":"trigger","version":2,"slug":"onSavePost1","settings":{"postQuery":{"postSource":"custom","postType":["post"],"postId":[],"postStatus":["publish"],"json":{"and":[{"==":[{"var":"{{onSavePost1.post.type}}"},"post"]},{"==":[{"var":"{{onSavePost1.post.status}}"},"publish"]}]},"natural":"{{onSavePost1.post.type}} is \'post\', and {{onSavePost1.post.status}} is \'publish\'"}}},"width":140,"height":68,"selected":true,"positionAbsolute":{"x":20,"y":-100},"dragging":false,"targetPosition":"top","sourcePosition":"bottom","$H":284,"x":12,"y":12},{"id":"n1717542934571","type":"generic","position":{"x":20,"y":140},"data":{"name":"action\/core.post-change-status","elementaryType":"action","version":1,"slug":"changePostStatus1","settings":{"newStatus":{"newStatus":"publish","status":"draft"},"post":{"variable":"{{onSavePost1.post}}"}}},"width":140,"height":68,"selected":false,"positionAbsolute":{"x":20,"y":140},"dragging":false,"targetPosition":"top","sourcePosition":"bottom","$H":286,"x":12,"y":252},{"id":"n1717542946349","type":"generic","position":{"x":20,"y":20},"data":{"name":"advanced\/core.schedule","elementaryType":"advanced","version":1,"slug":"schedule1","settings":{"schedule":{"whenToRun":"offset","dateSource":"onSavePost1.post.post_date","recurrence":"single","repeatUntil":"forever","repeatInterval":"3600","repeatTimes":"5","repeatUntilDate":"2024-06-11T23:16:24.422Z","unique":true,"priority":"10","specificDate":"2024-06-07T23:16:24.422Z","dateOffset":"+15 days"}}},"width":140,"height":68,"selected":false,"positionAbsolute":{"x":20,"y":20},"dragging":false,"targetPosition":"top","sourcePosition":"bottom","$H":288,"x":12,"y":132},{"id":"n1717543017933","type":"generic","position":{"x":0,"y":250},"data":{"name":"action\/core.replace-post-terms","elementaryType":"action","version":1,"slug":"replacePostTerms1","settings":{"taxonomyTerms":{"taxonomy":"category","terms":[]},"post":{"variable":"{{onSavePost1.post}}"}}},"width":163,"height":68,"selected":false,"positionAbsolute":{"x":0,"y":250},"dragging":false},{"id":"n1717543063491","type":"generic","position":{"x":20,"y":360},"data":{"name":"action\/core.send-email","elementaryType":"action","version":1,"slug":"sendEmail1","settings":{"recipient":{"recipient":"global.site.admin_email","custom":""}}},"width":140,"height":68,"selected":false,"positionAbsolute":{"x":20,"y":360},"dragging":false}],"edges":[{"source":"n1717542854679","sourceHandle":"output","target":"n1717542946349","targetHandle":"input","type":"genericEdge","id":"reactflow__edge-n1717542854679output-n1717542946349input","sections":[{"id":"reactflow__edge-n1717542854679output-n1717542946349input_s0","startPoint":{"x":87,"y":62},"endPoint":{"x":87,"y":132},"incomingShape":"n1717542854679","outgoingShape":"n1717542946349"}],"container":"root","selected":false,"markerEnd":{"type":"arrowclosed"}},{"source":"n1717542946349","sourceHandle":"output","target":"n1717542934571","targetHandle":"input","type":"genericEdge","id":"n1717542946349-output-n1717542934571-input","sections":[{"id":"n1717542946349-output-n1717542934571-input_s0","startPoint":{"x":87,"y":182},"endPoint":{"x":87,"y":252},"incomingShape":"n1717542946349","outgoingShape":"n1717542934571"}],"container":"root","selected":false,"markerEnd":{"type":"arrowclosed"}},{"source":"n1717542934571","sourceHandle":"output","target":"n1717543017933","targetHandle":"input","type":"genericEdge","id":"n1717542934571-output-n1717543017933-input","markerEnd":{"type":"arrowclosed"}},{"source":"n1717543017933","sourceHandle":"output","target":"n1717543063491","targetHandle":"input","type":"genericEdge","id":"n1717543017933-output-n1717543063491-input","markerEnd":{"type":"arrowclosed"}}],"viewport":{"x":403,"y":659.5,"zoom":2},"editorVersion":"4.5.0-beta.2"}', // phpcs:ignore Generic.Files.LineLength.TooLong
            ],
            [
                'title' => __('Add extra categories one month after publishing', 'post-expirator'),
                'description' => __('Sample workflow to add extra categories one month after publishing', 'post-expirator'), // phpcs:ignore Generic.Files.LineLength.TooLong
                'flow' => '{"nodes":[{"id":"n1717531365606","type":"trigger","position":{"x":12,"y":12},"data":{"name":"trigger\/core.save-post","elementaryType":"trigger","version":2,"slug":"onSavePost1","settings":{"postQuery":{"postSource":"custom","postType":["post"],"postId":[],"postStatus":["publish"],"json":{"and":[{"==":[{"var":"{{onSavePost1.post.type}}"},"post"]},{"==":[{"var":"{{onSavePost1.post.status}}"},"publish"]}]},"natural":"{{onSavePost1.post.type}} is \'post\', and {{onSavePost1.post.status}} is \'publish\'"}}},"width":140,"height":68,"targetPosition":"top","sourcePosition":"bottom","$H":284,"x":12,"y":12,"selected":true,"positionAbsolute":{"x":12,"y":12},"dragging":false},{"id":"n1717531386494","type":"generic","position":{"x":12,"y":132},"data":{"name":"advanced\/core.schedule","elementaryType":"advanced","version":1,"slug":"schedule1","settings":{"schedule":{"whenToRun":"offset","dateSource":"onSavePost1.post.post_date","recurrence":"single","repeatUntil":"forever","repeatInterval":"3600","repeatTimes":"5","repeatUntilDate":"2024-06-11T20:03:33.442Z","unique":true,"priority":"10","specificDate":"2024-06-07T20:03:33.442Z","dateOffset":"+30 days"}}},"width":140,"height":68,"selected":false,"positionAbsolute":{"x":12,"y":132},"dragging":false,"targetPosition":"top","sourcePosition":"bottom","$H":333,"x":12,"y":132},{"id":"n1717531395486","type":"generic","position":{"x":12,"y":252},"data":{"name":"action\/core.add-post-terms","elementaryType":"action","version":1,"slug":"addPostTerms1","settings":{"taxonomyTerms":{"taxonomy":"category","terms":[]},"post":{"variable":"{{onSavePost1.post}}"}}},"width":155,"height":68,"targetPosition":"top","sourcePosition":"bottom","$H":335,"x":12,"y":252,"selected":false,"positionAbsolute":{"x":12,"y":252}},{"id":"n1717531398637","type":"generic","position":{"x":12,"y":372},"data":{"name":"action\/core.send-email","elementaryType":"action","version":1,"slug":"sendEmail1","settings":{"recipient":{"recipient":"global.site.admin_email","custom":""}}},"width":140,"height":68,"targetPosition":"top","sourcePosition":"bottom","$H":337,"x":12,"y":372,"selected":false,"dragging":false,"positionAbsolute":{"x":12,"y":372}}],"edges":[{"source":"n1717531365606","sourceHandle":"output","target":"n1717531386494","targetHandle":"input","type":"genericEdge","id":"n1717531365606-output-n1717531386494-input","markerEnd":{"type":"arrowclosed"}},{"source":"n1717531386494","sourceHandle":"output","target":"n1717531395486","targetHandle":"input","type":"genericEdge","id":"n1717531386494-output-n1717531395486-input","markerEnd":{"type":"arrowclosed"}},{"source":"n1717531395486","sourceHandle":"output","target":"n1717531398637","targetHandle":"input","type":"genericEdge","id":"n1717531395486-output-n1717531398637-input","markerEnd":{"type":"arrowclosed"}}],"viewport":{"x":406,"y":550.5,"zoom":2},"editorVersion":"4.5.0-beta.2"}', // phpcs:ignore Generic.Files.LineLength.TooLong
            ]
        ];
    }
}
