---
# Create Incus containers for Veza V5 Ultra deployment
# Creates all necessary containers with proper networking

- name: Create Incus containers for Veza V5 Ultra
  hosts: edge
  become: true
  gather_facts: true

  vars:
    containers:
      - name: veza-haproxy
        image: debian/bookworm
        profile: veza
        cpu: 2
        memory: 2GB
        disk: 10GB
        ip: 10.10.0.100
        ports:
          - "80:80"
          - "443:443"
      - name: veza-backend
        image: debian/bookworm
        profile: veza
        cpu: 4
        memory: 4GB
        disk: 20GB
        ip: 10.10.0.101
        ports:
          - "8080:8080"
      - name: veza-chat
        image: debian/bookworm
        profile: veza
        cpu: 2
        memory: 2GB
        disk: 10GB
        ip: 10.10.0.102
        ports:
          - "8081:8081"
      - name: veza-stream
        image: debian/bookworm
        profile: veza
        cpu: 2
        memory: 2GB
        disk: 20GB
        ip: 10.10.0.103
        ports:
          - "8082:8082"
      - name: veza-web
        image: debian/bookworm
        profile: veza
        cpu: 2
        memory: 2GB
        disk: 10GB
        ip: 10.10.0.104
        ports:
          - "3000:3000"

  tasks:
    - name: Create Veza containers
      command: |
        incus launch {{ item.image }} {{ item.name }} \
          --profile {{ item.profile }} \
          --config limits.cpu={{ item.cpu }} \
          --config limits.memory={{ item.memory }} \
          --config limits.disk={{ item.disk }} \
          --config boot.autostart=true \
          --config boot.autostart.delay=10
      register: container_create_result
      failed_when: false
      loop: "{{ containers }}"

    - name: Display container creation results
      debug:
        msg: "Container {{ item.item.name }}: {{ 'Created' if item.rc == 0 else 'Failed' }}"
      loop: "{{ container_create_result.results }}"

    - name: Configure container networking
      command: |
        incus config device set {{ item.name }} eth0 ipv4.address={{ item.ip }}/24
      register: network_config_result
      failed_when: false
      loop: "{{ containers }}"

    - name: Display networking results
      debug:
        msg: "Network config {{ item.item.name }}: {{ 'Success' if item.rc == 0 else 'Failed' }}"
      loop: "{{ network_config_result.results }}"

    - name: Add proxy devices for external access
      command: |
        incus config device add {{ item.name }} proxy{{ loop.index0 }} proxy \
          listen=tcp:0.0.0.0:{{ port.split(':')[0] }} \
          connect=tcp:127.0.0.1:{{ port.split(':')[1] }}
      register: proxy_result
      failed_when: false
      loop: "{{ containers }}"
      vars:
        port_list: "{{ item.ports | default([]) }}"
      when: item.ports is defined and item.ports | length > 0

    - name: Start all containers
      command: incus start {{ item.name }}
      register: start_result
      failed_when: false
      loop: "{{ containers }}"

    - name: Display start results
      debug:
        msg: "Container {{ item.item.name }}: {{ 'Started' if item.rc == 0 else 'Failed to start' }}"
      loop: "{{ start_result.results }}"

    - name: Wait for containers to be ready
      wait_for:
        port: 22
        host: "{{ item.ip }}"
        timeout: 60
      register: container_ready
      failed_when: false
      loop: "{{ containers }}"

    - name: Display container readiness
      debug:
        msg: "Container {{ item.item.name }} ({{ item.item.ip }}): {{ 'Ready' if item.skipped else 'Not ready' }}"
      loop: "{{ container_ready.results }}"

    - name: List all containers
      command: incus list
      register: container_list

    - name: Display container list
      debug:
        var: container_list.stdout_lines

    - name: Show container network configuration
      command: incus network show veza-ovn
      register: network_show

    - name: Display network configuration
      debug:
        var: network_show.stdout_lines

  post_tasks:
    - name: Verify all containers are running
      command: incus list --format=json
      register: containers_json

    - name: Display running containers
      debug:
        msg: "Running containers: {{ containers_json.stdout | from_json | map(attribute='name') | list }}"