---
- name: Check if Airflow master deployment directory exists
  stat:
    path: "{{ airflow_master_dir }}"
  register: master_dir_stat

- name: Ensure Airflow master deployment directory exists
  file:
    path: "{{ airflow_master_dir }}"
    state: directory
    owner: "{{ ssh_user }}"
    group: ytdl
    mode: '0755'
  become: yes
  when: not master_dir_stat.stat.exists

- name: Check if source directories exist
  stat:
    path: "{{ playbook_dir }}/../{{ item }}"
  register: source_dirs
  loop:
    - "airflow/inputfiles"
    - "airflow/plugins"
    - "airflow/addfiles"
    - "airflow/bgutil-ytdlp-pot-provider"

- name: Sync Airflow master files
  synchronize:
    src: "{{ playbook_dir }}/../{{ item }}"
    dest: "{{ airflow_master_dir }}/"
    archive: yes
    recursive: yes
    delete: yes
    rsync_opts: "{{ rsync_default_opts }}"
  loop:
    - "airflow/Dockerfile"
    - "airflow/docker-compose-master.yaml"
    - "airflow/dags/"
    - "airflow/config/"
    - "setup.py"
    - "yt_ops_services/"
    - "thrift_model/"
    - "VERSION"
    - "airflow/init-airflow.sh"
    - "airflow/nginx.conf"
    - "get_info_json_client.py"
    - "proxy_manager_client.py"

- name: Sync optional directories if they exist
  synchronize:
    src: "{{ playbook_dir }}/../{{ item }}/"
    dest: "{{ airflow_master_dir }}/{{ item | basename }}/"
    archive: yes
    recursive: yes
    delete: yes
    rsync_opts: "{{ rsync_default_opts }}"
  loop:
    - "airflow/inputfiles"
    - "airflow/plugins"
    - "airflow/addfiles"
    - "airflow/bgutil-ytdlp-pot-provider"
  when: source_dirs.results | selectattr('item', 'equalto', item) | map(attribute='stat.exists') | first

- name: Sync pangramia thrift files
  synchronize:
    src: "{{ playbook_dir }}/../thrift_model/gen_py/pangramia/"
    dest: "{{ airflow_master_dir }}/pangramia/"
    archive: yes
    recursive: yes
    delete: yes
    rsync_opts: "{{ rsync_default_opts }}"

- name: Create .env file for Airflow master service
  template:
    src: "../../templates/.env.master.j2"
    dest: "{{ airflow_master_dir }}/.env"
    mode: "{{ file_permissions }}"
    owner: "{{ ssh_user }}"
    group: ytdl
  vars:
    service_role: "master"

- name: Create symlink for docker-compose.yaml
  file:
    src: "{{ airflow_master_dir }}/docker-compose-master.yaml"
    dest: "{{ airflow_master_dir }}/docker-compose.yaml"
    state: link
    owner: "{{ ssh_user }}"
    group: ytdl
    force: yes

- name: Verify Dockerfile exists in build directory
  stat:
    path: "{{ airflow_master_dir }}/Dockerfile"
  register: dockerfile_stat

- name: Fail if Dockerfile is missing
  fail:
    msg: "Dockerfile not found in {{ airflow_master_dir }}. Cannot build image."
  when: not dockerfile_stat.stat.exists

- name: Build Airflow master image
  community.docker.docker_image:
    name: "{{ airflow_image_name }}"
    build:
      path: "{{ airflow_master_dir }}"
      dockerfile: "Dockerfile"
    source: build
    force_source: true

- name: Run Airflow init script
  shell:
    cmd: "chmod +x init-airflow.sh && ./init-airflow.sh"
    chdir: "{{ airflow_master_dir }}"
  become: yes
  become_user: "{{ ssh_user }}"

- name: Start Airflow master service
  community.docker.docker_compose_v2:
    project_src: "{{ airflow_master_dir }}"
    files:
      - "docker-compose-master.yaml"
    state: present
    remove_orphans: true
