import json
from base64 import b64decode
from os import path as osp

import pytest

from infrahouse_toolkit.terraform.backends import TFS3Backend
from infrahouse_toolkit.terraform.status import RunOutput, RunResult, TFStatus


@pytest.mark.parametrize(
    "plan_file, result_counts, expected_comment",
    [
        (
            "plan-no-output.stdout",
            (1, 1, 1),
            """
# State **`s3://foo_backet/path/to/tf.state`**
## Affected resources counts

|   Success |   🟢 Add |   🟡 Change |   🔴 Destroy |
|----------:|---------:|------------:|-------------:|
|        ✅ |        1 |           1 |            1 |
<details>
<summary>STDOUT</summary>

```
no stdout
```
</details>
<details><summary><i>metadata</i></summary>

```
eyJzMzovL2Zvb19iYWNrZXQvcGF0aC90by90Zi5zdGF0ZSI6IHsic3VjY2VzcyI6IHRydWUsICJhZGQiOiAxLCAiY2hhbmdlIjogMSwgImRlc3Ryb3kiOiAxfX0=
```
</details>""",
        ),
        (
            "plan-0-0-0.stdout",
            (0, 0, 0),
            """
# State **`s3://foo_backet/path/to/tf.state`**
## Affected resources counts

|   Success |    Add |    Change |    Destroy |
|----------:|-------:|----------:|-----------:|
|        ✅ |      0 |         0 |          0 |
<details>
<summary>STDOUT</summary>

```
Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.
```
</details>
<details><summary><i>metadata</i></summary>

```
eyJzMzovL2Zvb19iYWNrZXQvcGF0aC90by90Zi5zdGF0ZSI6IHsic3VjY2VzcyI6IHRydWUsICJhZGQiOiAwLCAiY2hhbmdlIjogMCwgImRlc3Ryb3kiOiAwfX0=
```
</details>""",
        ),
        (
            "plan-2-0-0.stdout",
            (2, 0, 0),
            """
# State **`s3://foo_backet/path/to/tf.state`**
## Affected resources counts

|   Success |   🟢 Add |    Change |    Destroy |
|----------:|---------:|----------:|-----------:|
|        ✅ |        2 |         0 |          0 |
<details>
<summary>STDOUT</summary>

```
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # module.repos["test"].github_repository.repo will be created
  + resource "github_repository" "repo" {
      + allow_auto_merge            = false
      + allow_merge_commit          = true
      + allow_rebase_merge          = true
      + allow_squash_merge          = true
      + archived                    = false
      + default_branch              = (known after apply)
      + delete_branch_on_merge      = false
      + description                 = "Template for a GitHub Control repository"
      + etag                        = (known after apply)
      + full_name                   = (known after apply)
      + git_clone_url               = (known after apply)
      + has_issues                  = true
      + html_url                    = (known after apply)
      + http_clone_url              = (known after apply)
      + id                          = (known after apply)
      + merge_commit_message        = "PR_TITLE"
      + merge_commit_title          = "MERGE_MESSAGE"
      + name                        = "test"
      + node_id                     = (known after apply)
      + private                     = (known after apply)
      + repo_id                     = (known after apply)
      + squash_merge_commit_message = "COMMIT_MESSAGES"
      + squash_merge_commit_title   = "COMMIT_OR_PR_TITLE"
      + ssh_clone_url               = (known after apply)
      + svn_url                     = (known after apply)
      + visibility                  = "public"

      + security_and_analysis {
          + advanced_security {
              + status = (known after apply)
            }

          + secret_scanning {
              + status = (known after apply)
            }

          + secret_scanning_push_protection {
              + status = (known after apply)
            }
        }
    }

  # module.repos["test"].github_team_repository.dev will be created
  + resource "github_team_repository" "dev" {
      + etag       = (known after apply)
      + id         = (known after apply)
      + permission = "push"
      + repository = "test"
      + team_id    = "7332815"
    }

Plan: 2 to add, 0 to change, 0 to destroy.

─────────────────────────────────────────────────────────────────────────────

Saved the plan to: tf.plan

To perform exactly these actions, run the following command to apply:
    terraform apply "tf.plan"
```
</details>
<details><summary><i>metadata</i></summary>

```
eyJzMzovL2Zvb19iYWNrZXQvcGF0aC90by90Zi5zdGF0ZSI6IHsic3VjY2VzcyI6IHRydWUsICJhZGQiOiAyLCAiY2hhbmdlIjogMCwgImRlc3Ryb3kiOiAwfX0=
```
</details>""",
        ),
        (
            "plan-2-1-2.stdout",
            (2, 1, 2),
            """
# State **`s3://foo_backet/path/to/tf.state`**
## Affected resources counts

|   Success |   🟢 Add |   🟡 Change |   🔴 Destroy |
|----------:|---------:|------------:|-------------:|
|        ✅ |        2 |           1 |            2 |
<details>
<summary>STDOUT</summary>

```
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create
  ~ update in-place
  - destroy

Terraform will perform the following actions:

  # module.repos["cookiecutter-github-control"].github_repository.repo will be destroyed
  # (because module.repos["cookiecutter-github-control"] is not in configuration)
  - resource "github_repository" "repo" {
      - allow_auto_merge            = false -> null
      - allow_merge_commit          = true -> null
      - allow_rebase_merge          = true -> null
      - allow_squash_merge          = true -> null
      - allow_update_branch         = false -> null
      - archived                    = false -> null
      - default_branch              = "main" -> null
      - delete_branch_on_merge      = false -> null
      - description                 = "Template for a GitHub Control repository" -> null
      - etag                        = "W/\\\"8b4a792bc1474d381caaa63b76668993b4adc42ae76ed53d6886d8562ebb0c67\\\"" -> null
      - full_name                   = "infrahouse/cookiecutter-github-control" -> null
      - git_clone_url               = "git://github.com/infrahouse/cookiecutter-github-control.git" -> null
      - has_discussions             = false -> null
      - has_downloads               = false -> null
      - has_issues                  = true -> null
      - has_projects                = false -> null
      - has_wiki                    = false -> null
      - html_url                    = "https://github.com/infrahouse/cookiecutter-github-control" -> null
      - http_clone_url              = "https://github.com/infrahouse/cookiecutter-github-control.git" -> null
      - id                          = "cookiecutter-github-control" -> null
      - is_template                 = false -> null
      - merge_commit_message        = "PR_TITLE" -> null
      - merge_commit_title          = "MERGE_MESSAGE" -> null
      - name                        = "cookiecutter-github-control" -> null
      - node_id                     = "R_kgDOI528zg" -> null
      - private                     = false -> null
      - repo_id                     = 597540046 -> null
      - squash_merge_commit_message = "COMMIT_MESSAGES" -> null
      - squash_merge_commit_title   = "COMMIT_OR_PR_TITLE" -> null
      - ssh_clone_url               = "git@github.com:infrahouse/cookiecutter-github-control.git" -> null
      - svn_url                     = "https://github.com/infrahouse/cookiecutter-github-control" -> null
      - topics                      = [] -> null
      - visibility                  = "public" -> null
      - vulnerability_alerts        = false -> null

      - security_and_analysis {

          - secret_scanning {
              - status = "disabled" -> null
            }

          - secret_scanning_push_protection {
              - status = "disabled" -> null
            }
        }
    }

  # module.repos["cookiecutter-github-control"].github_team_repository.dev will be destroyed
  # (because module.repos["cookiecutter-github-control"] is not in configuration)
  - resource "github_team_repository" "dev" {
      - etag       = "W/\\\"8043f81b19693f6c1a72d21bb8dc03859c98bf78bfbe79782bfe13fa813992ca\\\"" -> null
      - id         = "7332815:cookiecutter-github-control" -> null
      - permission = "push" -> null
      - repository = "cookiecutter-github-control" -> null
      - team_id    = "7332815" -> null
    }

  # module.repos["infrahouse-toolkit"].github_repository.repo will be updated in-place
  ~ resource "github_repository" "repo" {
      ~ description                 = "InfraHouse Toolkit" -> "InfraHouse Toolkit1"
        id                          = "infrahouse-toolkit"
        name                        = "infrahouse-toolkit"
        # (31 unchanged attributes hidden)

        # (1 unchanged block hidden)
    }

  # module.repos["test"].github_repository.repo will be created
  + resource "github_repository" "repo" {
      + allow_auto_merge            = false
      + allow_merge_commit          = true
      + allow_rebase_merge          = true
      + allow_squash_merge          = true
      + archived                    = false
      + default_branch              = (known after apply)
      + delete_branch_on_merge      = false
      + description                 = "Template for a GitHub Control repository"
      + etag                        = (known after apply)
      + full_name                   = (known after apply)
      + git_clone_url               = (known after apply)
      + has_issues                  = true
      + html_url                    = (known after apply)
      + http_clone_url              = (known after apply)
      + id                          = (known after apply)
      + merge_commit_message        = "PR_TITLE"
      + merge_commit_title          = "MERGE_MESSAGE"
      + name                        = "test"
      + node_id                     = (known after apply)
      + private                     = (known after apply)
      + repo_id                     = (known after apply)
      + squash_merge_commit_message = "COMMIT_MESSAGES"
      + squash_merge_commit_title   = "COMMIT_OR_PR_TITLE"
      + ssh_clone_url               = (known after apply)
      + svn_url                     = (known after apply)
      + visibility                  = "public"

      + security_and_analysis {
          + advanced_security {
              + status = (known after apply)
            }

          + secret_scanning {
              + status = (known after apply)
            }

          + secret_scanning_push_protection {
              + status = (known after apply)
            }
        }
    }

  # module.repos["test"].github_team_repository.dev will be created
  + resource "github_team_repository" "dev" {
      + etag       = (known after apply)
      + id         = (known after apply)
      + permission = "push"
      + repository = "test"
      + team_id    = "7332815"
    }

Plan: 2 to add, 1 to change, 2 to destroy.

─────────────────────────────────────────────────────────────────────────────

Saved the plan to: tf.plan

To perform exactly these actions, run the following command to apply:
    terraform apply "tf.plan"
```
</details>
<details><summary><i>metadata</i></summary>

```
eyJzMzovL2Zvb19iYWNrZXQvcGF0aC90by90Zi5zdGF0ZSI6IHsic3VjY2VzcyI6IHRydWUsICJhZGQiOiAyLCAiY2hhbmdlIjogMSwgImRlc3Ryb3kiOiAyfX0=
```
</details>""",
        ),
        (
            "plan-0-2-0-a.stdout",
            (0, 2, 0),
            """
# State **`s3://foo_backet/path/to/tf.state`**
## Affected resources counts

|   Success |    Add |   🟡 Change |    Destroy |
|----------:|-------:|------------:|-----------:|
|        ✅ |      0 |           2 |          0 |
<details>
<summary>STDOUT</summary>

```
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # module.website.aws_autoscaling_group.website will be updated in-place
  ~ resource "aws_autoscaling_group" "website" {
        id                               = "web20231125205239428700000003"
        name                             = "web20231125205239428700000003"
        # (32 unchanged attributes hidden)

      ~ launch_template {
            id      = "lt-042ea5dd55b0fff3b"
            name    = "web20231125205151213200000001"
          ~ version = "8" -> (known after apply)
        }

        # (9 unchanged blocks hidden)
    }

  # module.website.aws_launch_template.website will be updated in-place
  ~ resource "aws_launch_template" "website" {
        id                                   = "lt-042ea5dd55b0fff3b"
      ~ latest_version                       = 8 -> (known after apply)
        name                                 = "web20231125205151213200000001"
        tags                                 = {}
      ~ user_data                            = " " -> (known after apply)
userdata changes:
--- before
+++ after
@@ -1,109 +1 @@
-Content-Type: multipart/mixed; boundary="MIMEBOUNDARY"
-MIME-Version: 1.0
-
---MIMEBOUNDARY
-Content-Transfer-Encoding: 7bit
-Content-Type: text/cloud-config
-Mime-Version: 1.0
-
-#cloud-config
-"apt":
-  "sources":
-    "infrahouse":
-      "key": |
-        -----BEGIN PGP PUBLIC KEY BLOCK-----
-
-        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
-        55pMorQyUTEQCQ3ECzHYSnL/TmW7
-        =+0Rc
-        -----END PGP PUBLIC KEY BLOCK-----
-      "source": "deb [signed-by=$KEY_FILE] https://release-jammy.infrahouse.com/ $RELEASE
-        main"
-"package_update": true
-"packages":
-- "make"
-- "gcc"
-- "puppet-code"
-- "infrahouse-toolkit"
-- "infrahouse-puppet-data"
-"puppet":
-  "collection": "puppet8"
-  "install": true
-  "install_type": "aio"
-  "package_name": "puppet-agent"
-  "start_service": false
-"runcmd":
-- "/opt/puppetlabs/puppet/bin/gem install json"
-- "/opt/puppetlabs/puppet/bin/gem install aws-sdk-core"
-- "/opt/puppetlabs/puppet/bin/gem install aws-sdk-secretsmanager"
-- "ih-puppet  --environment production --environmentpath {root_directory}/environments
-  --root-directory /opt/puppet-code --hiera-config /opt/infrahouse-puppet-data/environments/production/hiera.yaml
-  --module-path {root_directory}/modules apply /opt/puppet-code/environments/production/manifests/site.pp"
-"write_files":
-- "content": "export AWS_DEFAULT_REGION=us-west-1"
-  "path": "/etc/profile.d/aws.sh"
-  "permissions": "0644"
-- "content": |-
-    [default]
-    region=us-west-1
-  "path": "/root/.aws/config"
-  "permissions": "0600"
-- "content": |
-    "puppet_environment": "production"
-    "puppet_role": "webserver"
-  "path": "/etc/puppetlabs/facter/facts.d/puppet.yaml"
-  "permissions": "0644"
-- "content": "{\\"ih-puppet\\":{\\"debug\\":false,\\"environmentpath\\":\\"{root_directory}/environments\\",\\"hiera-config\\":\\"/opt/infrahouse-puppet-data/environments/production/hiera.yaml\\",\\"manifest\\":\\"/opt/puppet-code/environments/production/manifests/site.pp\\",\\"module-path\\":\\"{root_directory}/modules\\",\\"root-directory\\":\\"/opt/puppet-code\\"}}"
-  "path": "/etc/puppetlabs/facter/facts.d/ih-puppet.json"
-  "permissions": "0644"
-- "content": "{}"
-  "path": "/etc/puppetlabs/facter/facts.d/custom.json"
-  "permissions": "0644"
-
---MIMEBOUNDARY--
+(known after apply)
EOF userdata changes.
        # (16 unchanged attributes hidden)

      + metadata_options {
          + http_tokens = "required"
        }

        # (4 unchanged blocks hidden)
    }

Plan: 0 to add, 2 to change, 0 to destroy.

─────────────────────────────────────────────────────────────────────────────

Saved the plan to: tf.plan

To perform exactly these actions, run the following command to apply:
    terraform apply "tf.plan"
```
</details>
<details><summary><i>metadata</i></summary>

```
eyJzMzovL2Zvb19iYWNrZXQvcGF0aC90by90Zi5zdGF0ZSI6IHsic3VjY2VzcyI6IHRydWUsICJhZGQiOiAwLCAiY2hhbmdlIjogMiwgImRlc3Ryb3kiOiAwfX0=
```
</details>""",
        ),
        (
            "plan-0-2-0.stdout",
            (0, 2, 0),
            """
# State **`s3://foo_backet/path/to/tf.state`**
## Affected resources counts

|   Success |    Add |   🟡 Change |    Destroy |
|----------:|-------:|------------:|-----------:|
|        ✅ |      0 |           2 |          0 |
<details>
<summary>STDOUT</summary>

```
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # module.website.aws_autoscaling_group.website will be updated in-place
  ~ resource "aws_autoscaling_group" "website" {
        id                               = "web20231125205239428700000003"
        name                             = "web20231125205239428700000003"
        # (32 unchanged attributes hidden)

      ~ launch_template {
            id      = "lt-042ea5dd55b0fff3b"
            name    = "web20231125205151213200000001"
          ~ version = "8" -> (known after apply)
        }

        # (9 unchanged blocks hidden)
    }

  # module.website.aws_launch_template.website will be updated in-place
  ~ resource "aws_launch_template" "website" {
        id                                   = "lt-042ea5dd55b0fff3b"
      ~ latest_version                       = 8 -> (known after apply)
        name                                 = "web20231125205151213200000001"
        tags                                 = {}
      ~ user_data                            = " " -> " "
userdata changes:
--- before
+++ after
@@ -99,7 +99,7 @@
     "puppet_role": "webserver"
   "path": "/etc/puppetlabs/facter/facts.d/puppet.yaml"
   "permissions": "0644"
-- "content": "{\\"ih-puppet\\":{\\"debug\\":false,\\"environmentpath\\":\\"{root_directory}/environments\\",\\"hiera-config\\":\\"/opt/infrahouse-puppet-data/environments/production/hiera.yaml\\",\\"manifest\\":\\"/opt/puppet-code/environments/production/manifests/site.pp\\",\\"module-path\\":\\"{root_directory}/modules\\",\\"root-directory\\":\\"/opt/puppet-code\\"}}"
+- "content": "{\\"ih-puppet\\":{\\"cancel_instance_refresh_on_error\\":false,\\"debug\\":false,\\"environmentpath\\":\\"{root_directory}/environments\\",\\"hiera-config\\":\\"/opt/infrahouse-puppet-data/environments/production/hiera.yaml\\",\\"manifest\\":\\"/opt/puppet-code/environments/production/manifests/site.pp\\",\\"module-path\\":\\"{root_directory}/modules\\",\\"root-directory\\":\\"/opt/puppet-code\\"}}"
   "path": "/etc/puppetlabs/facter/facts.d/ih-puppet.json"
   "permissions": "0644"
 - "content": "{}"
EOF userdata changes.
        # (16 unchanged attributes hidden)

      + metadata_options {
          + http_tokens = "required"
        }

        # (4 unchanged blocks hidden)
    }

Plan: 0 to add, 2 to change, 0 to destroy.

─────────────────────────────────────────────────────────────────────────────

Saved the plan to: tf.plan

To perform exactly these actions, run the following command to apply:
    terraform apply "tf.plan"
```
</details>
<details><summary><i>metadata</i></summary>

```
eyJzMzovL2Zvb19iYWNrZXQvcGF0aC90by90Zi5zdGF0ZSI6IHsic3VjY2VzcyI6IHRydWUsICJhZGQiOiAwLCAiY2hhbmdlIjogMiwgImRlc3Ryb3kiOiAwfX0=
```
</details>""",
        ),
    ],
)
def test_comment(plan_file, result_counts, expected_comment):
    with open(osp.join("infrahouse_toolkit/terraform/tests/plans", plan_file)) as fp:
        status = TFStatus(
            TFS3Backend("foo_backet", "path/to/tf.state"), True, RunResult(*result_counts), RunOutput(fp.read(), None)
        )
        assert isinstance(status.comment, str)
        print("\nActual comment:")
        print(status.comment)
        print("EOF Actual comment.")
        assert status.comment == expected_comment


def test_comment_none():
    status = TFStatus(
        TFS3Backend("foo_backet", "path/to/tf.state"),
        True,
        RunResult(None, None, None),
        RunOutput("no stdout", "no stderr"),
        affected_resources=RunResult(None, None, None),
    )
    assert isinstance(status.comment, str)
    print(status.comment)
