---
- name: Deploy sepiatones_xyz project
  hosts: all
  become: yes
  vars:
    app_user: "sepiatones_xyz"
    app_home: "/home/{{ app_user }}"

  tasks:
    - name: Ensure user exists
      user:
        name: "{{ app_user }}"
        shell: /bin/bash
        create_home: yes
        home: "{{ app_home }}"

    - name: Create data directory
      file:
        path: "{{ app_home }}/data"
        state: directory
        owner: "{{ app_user }}"
        group: "{{ app_user }}"

    - name: Create templates directory
      file:
        path: "{{ app_home }}/templates"
        state: directory
        owner: "{{ app_user }}"
        group: "{{ app_user }}"

    - name: Install Bun
      shell: curl -fsSL https://bun.sh/install | bash
      args:
        creates: /home/{{ app_user }}/.bun/bin/bun
      become_user: "{{ app_user }}"

    - name: Create source directory
      file:
        path: "{{ app_home }}/src"
        state: directory
        owner: "{{ app_user }}"
        group: "{{ app_user }}"

    - name: Copy package.json
      copy:
        src: ../package.json
        dest: "{{ app_home }}/package.json"
        owner: "{{ app_user }}"
        group: "{{ app_user }}"

    - name: Copy bun.lockb
      copy:
        src: ../bun.lockb
        dest: "{{ app_home }}/bun.lockb"
        owner: "{{ app_user }}"
        group: "{{ app_user }}"

    - name: Copy source files
      synchronize:
        src: ../src/
        dest: "{{ app_home }}/src/"
        recursive: yes
        owner: no
        group: no
        archive: yes
        delete: yes

    - name: Install dependencies
      shell: /home/{{ app_user }}/.bun/bin/bun install --frozen-lockfile
      args:
        chdir: "{{ app_home }}"
      become_user: "{{ app_user }}"

    - name: Build binary
      shell: /home/{{ app_user }}/.bun/bin/bun build --compile --minify --target bun --outfile ./sepiatones_xyz ./src/index.tsx
      args:
        chdir: "{{ app_home }}"
      become_user: "{{ app_user }}"

    - name: Copy data directory
      synchronize:
        src: ../data/
        dest: "{{ app_home }}/data/"
        recursive: yes
        owner: no
        group: no
        archive: yes
        delete: yes

    - name: Copy templates directory
      synchronize:
        src: ../templates/
        dest: "{{ app_home }}/templates/"
        recursive: yes
        owner: no
        group: no
        archive: yes
        delete: yes

    - name: Ensure Caddy directory exists
      file:
        path: /etc/caddy/sites
        state: directory

    - name: Copy Caddy configuration
      copy:
        src: sepiatones_xyz.caddy
        dest: /etc/caddy/sites/sepiatones_xyz.caddy
        owner: root
        group: root
        mode: "0644"
      register: caddy_config_copy

    - name: Restart Caddy service if config changed
      systemd:
        name: caddy
        state: restarted
        daemon_reload: yes
      when: caddy_config_copy.changed

    - name: Ensure Supervisor directory exists
      file:
        path: /etc/supervisor/conf.d
        state: directory

    - name: Copy Supervisor configuration
      copy:
        src: sepiatones_xyz.supervisor
        dest: /etc/supervisor/conf.d/sepiatones_xyz.conf
        owner: root
        group: root
        mode: "0644"

    - name: Reload Supervisor
      supervisorctl:
        name: sepiatones_xyz
        state: present

    - name: Restart the service
      supervisorctl:
        name: sepiatones_xyz
        state: restarted
