---
- name: Clone Agent Repository
  become: yes
  become_user: jingrow
  git:
    repo: '{{ agent_repository_url }}'
    dest: /home/jingrow/agent/repo
    remote: upstream
    depth: 1
    force: yes
    clone: yes
    update: no
  register: clone_result
  retries: 3
  delay: 10
  until: clone_result is success
  ignore_errors: yes

- name: Clean up failed clone
  become: yes
  become_user: jingrow
  file:
    path: /home/jingrow/agent/repo
    state: absent
  when: clone_result is failed
  ignore_errors: yes

- name: Verify clone success
  become: yes
  become_user: jingrow
  stat:
    path: /home/jingrow/agent/repo/.git
  register: git_check
  retries: 3
  delay: 5
  until: git_check.stat.exists
  when: clone_result is success

- name: Fail if clone not successful
  fail:
    msg: "Failed to clone agent repository after multiple attempts"
  when: clone_result is failed or (clone_result is success and not git_check.stat.exists)

- name: Install Agent
  become: yes
  become_user: jingrow
  pip:
    name: file:///home/jingrow/agent/repo
    virtualenv: /home/jingrow/agent/env
    virtualenv_python: python3
    editable: yes
    extra_args: "--force-reinstall"

- name: Generate Agent Configuration File
  become: yes
  become_user: jingrow
  command: '/home/jingrow/agent/env/bin/agent setup config --name {{ server }} --workers {{ workers }} {% if proxy_ip is defined and proxy_ip is truthy %}--proxy-ip {{ proxy_ip }}{% endif %} {% if agent_sentry_dsn is defined and agent_sentry_dsn is truthy %}--sentry-dsn {{ agent_sentry_dsn }}{% endif %}'
  args:
    chdir: /home/jingrow/agent

- name: Setup Agent SQLite Database
  become: yes
  become_user: jingrow
  command: /home/jingrow/agent/env/bin/agent setup database
  args:
    chdir: /home/jingrow/agent

- name: Setup Agent Usage Tracker
  become: yes
  become_user: jingrow
  command: /home/jingrow/agent/env/bin/agent setup usage
  args:
    chdir: /home/jingrow/agent

- name: Setup Agent Site Analytics Tracker
  become: yes
  become_user: jingrow
  command: /home/jingrow/agent/env/bin/agent setup site-analytics
  args:
    chdir: /home/jingrow/agent

- name: Create Agent NGINX Configuration Directory
  become: yes
  become_user: jingrow
  file:
    dest: /home/jingrow/agent/nginx
    state: directory

- name: Setup Agent Authentication
  become: yes
  become_user: jingrow
  command: '/home/jingrow/agent/env/bin/agent setup authentication --password {{ agent_password }}'
  args:
    chdir: /home/jingrow/agent

- name: Symlink Agent Supervisor Configuration
  file:
    src: /home/jingrow/agent/supervisor.conf
    dest: /etc/supervisor/conf.d/agent.conf
    state: link
    force: yes
    follow: no

- name: Create Logs Directory for Supervisor
  become: yes
  become_user: jingrow
  file:
    dest: /home/jingrow/agent/logs
    state: directory

- name: Setup Agent Supervisor
  become: yes
  become_user: jingrow
  command: /home/jingrow/agent/env/bin/agent setup supervisor
  args:
    chdir: /home/jingrow/agent

- name: Create NGINX Root Configuration File
  become: yes
  become_user: jingrow
  file:
    path: /home/jingrow/agent/nginx/nginx.conf
    state: touch

- name: Symlink NGINX Root Configuration File
  file:
    src: /home/jingrow/agent/nginx/nginx.conf
    dest: /etc/nginx/nginx.conf
    state: link
    force: yes
    follow: no

- name: Create Agent NGINX Configuration File
  become: yes
  become_user: jingrow
  file:
    path: /home/jingrow/agent/nginx.conf
    state: touch

- name: Symlink Agent NGINX Configuration File
  file:
    src: /home/jingrow/agent/nginx.conf
    dest: /etc/nginx/conf.d/agent.conf
    state: link
    force: yes
    follow: no

- name: Create Agent TLS Directory
  become: yes
  become_user: jingrow
  file:
    dest: /home/jingrow/agent/tls
    state: directory

- name: Setup Agent TLS (Private Key)
  become: yes
  become_user: jingrow
  copy:
    content: '{{ certificate_private_key }}'
    dest: /home/jingrow/agent/tls/privkey.pem

- name: Setup Agent TLS (Full Chain)
  become: yes
  become_user: jingrow
  copy:
    content: '{{ certificate_full_chain }}'
    dest: /home/jingrow/agent/tls/fullchain.pem

- name: Setup Agent TLS (Intermediate Chain)
  become: yes
  become_user: jingrow
  copy:
    content: '{{ certificate_intermediate_chain }}'
    dest: /home/jingrow/agent/tls/chain.pem

- name: Setup Agent NGINX
  become: yes
  become_user: jingrow
  command: /home/jingrow/agent/env/bin/agent setup nginx
  args:
    chdir: /home/jingrow/agent

- name: Setup Monitoring Authentication
  become: yes
  become_user: jingrow
  command: 'htpasswd -Bbc /home/jingrow/agent/nginx/monitoring.htpasswd jingrow {{ monitoring_password }}'
