---
# Verify that Forgejo deployment was successful

- name: Wait for Forgejo deployment to be ready
  kubernetes.core.k8s_info:
    api_version: apps/v1
    kind: Deployment
    name: "{{ forgejo_helm_release_name }}"
    namespace: "{{ forgejo_namespace }}"
    kubeconfig: "{{ k8s_auth_params.kubeconfig }}"
    validate_certs: "{{ k8s_auth_params.validate_certs }}"
  register: forgejo_deployment
  until: forgejo_deployment.resources[0].status.readyReplicas is defined and forgejo_deployment.resources[0].status.readyReplicas > 0
  retries: 30
  delay: 10
  tags:
    - forgejo
    - verify

- name: Get Forgejo route
  kubernetes.core.k8s_info:
    api_version: route.openshift.io/v1
    kind: Route
    namespace: "{{ forgejo_namespace }}"
    label_selectors:
      - "app.kubernetes.io/name=forgejo"
    kubeconfig: "{{ k8s_auth_params.kubeconfig }}"
    validate_certs: "{{ k8s_auth_params.validate_certs }}"
  register: forgejo_route
  tags:
    - forgejo
    - verify

- name: Display Forgejo route
  debug:
    msg: "Forgejo is available at: https://{{ forgejo_route.resources[0].spec.host if forgejo_route.resources | length > 0 else 'not available yet' }}"
  tags:
    - forgejo
    - verify

- name: Verify Forgejo services
  kubernetes.core.k8s_info:
    api_version: v1
    kind: Service
    namespace: "{{ forgejo_namespace }}"
    label_selectors:
      - "app.kubernetes.io/name=forgejo"
    kubeconfig: "{{ k8s_auth_params.kubeconfig }}"
    validate_certs: "{{ k8s_auth_params.validate_certs }}"
  register: forgejo_services
  tags:
    - forgejo
    - verify

- name: Display Forgejo services
  debug:
    msg: "Found {{ forgejo_services.resources | length }} Forgejo services"
  tags:
    - forgejo
    - verify

- name: Check if Forgejo pods are running
  kubernetes.core.k8s_info:
    api_version: v1
    kind: Pod
    namespace: "{{ forgejo_namespace }}"
    label_selectors:
      - app.kubernetes.io/name=forgejo
    kubeconfig: "{{ k8s_auth_params.kubeconfig }}"
    validate_certs: "{{ k8s_auth_params.validate_certs }}"
  register: forgejo_pods

- name: Show Forgejo pod status
  ansible.builtin.debug:
    msg: "Forgejo pod status: {{ item.status.phase }}, name: {{ item.metadata.name }}"
  loop: "{{ forgejo_pods.resources }}"
  when: forgejo_pods.resources is defined

- name: Check if PostgreSQL is running
  kubernetes.core.k8s_info:
    api_version: v1
    kind: Pod
    namespace: "{{ forgejo_namespace }}"
    label_selectors:
      - app.kubernetes.io/name=postgresql
    kubeconfig: "{{ k8s_auth_params.kubeconfig }}"
    validate_certs: "{{ k8s_auth_params.validate_certs }}"
  register: postgresql_pods

- name: Show PostgreSQL pod status
  ansible.builtin.debug:
    msg: "PostgreSQL pod status: {{ item.status.phase }}, name: {{ item.metadata.name }}"
  loop: "{{ postgresql_pods.resources }}"
  when: postgresql_pods.resources is defined

- name: Verify Forgejo service
  kubernetes.core.k8s_info:
    api_version: v1
    kind: Service
    namespace: "{{ forgejo_namespace }}"
    name: "{{ forgejo_helm_release_name }}"
    kubeconfig: "{{ k8s_auth_params.kubeconfig }}"
    validate_certs: "{{ k8s_auth_params.validate_certs }}"
  register: forgejo_service

- name: Show Forgejo service details
  ansible.builtin.debug:
    msg: "Forgejo service: {{ forgejo_service.resources[0].metadata.name }} running on port {{ forgejo_service.resources[0].spec.ports[0].port }}"
  when: forgejo_service.resources is defined and forgejo_service.resources | length > 0

- name: Check Forgejo route
  kubernetes.core.k8s_info:
    api_version: route.openshift.io/v1
    kind: Route
    namespace: "{{ forgejo_namespace }}"
    kubeconfig: "{{ k8s_auth_params.kubeconfig }}"
    validate_certs: "{{ k8s_auth_params.validate_certs }}"
  register: forgejo_routes

- name: Show Forgejo route details
  ansible.builtin.debug:
    msg: "Forgejo available at: https://{{ item.spec.host }}"
  loop: "{{ forgejo_routes.resources }}"
  when: forgejo_routes.resources is defined
