---
- name: Ensure signage user exists
  ansible.builtin.user:
    name: "{{ signage_user }}"
    create_home: true
    state: present
  become: true

- name: Ensure .ssh directory exists for signage user
  ansible.builtin.file:
    path: "/home/{{ signage_user }}/.ssh"
    state: directory
    owner: "{{ signage_user }}"
    group: "{{ signage_user }}"
    mode: "0700"
  become: true

- name: Deploy SSH public key for signage user
  ansible.builtin.authorized_key:
    user: "{{ signage_user }}"
    key: "{{ signage_ssh_public_key }}"
    state: present
  become: true
  when: signage_ssh_public_key | length > 0

- name: Ensure config directory exists
  ansible.builtin.file:
    path: "{{ signage_config_dir }}"
    state: directory
    owner: root
    group: root
    mode: "0755"
  become: true

- name: Deploy vars.yml template for player config
  ansible.builtin.template:
    src: vars.yml.j2
    dest: "{{ signage_config_dir }}/vars.yml"
    owner: root
    group: "{{ signage_user }}"
    mode: "0640"
  become: true

- name: Register screen at server via API
  ansible.builtin.uri:
    url: "{{ signage_server_base_url }}/api/v1/screens/register"
    method: POST
    body_format: json
    body:
      slug: "{{ screen_id }}"
      name: "{{ screen_name | default(screen_id) }}"
      orientation: "{{ screen_orientation | default('landscape') }}"
    headers:
      Content-Type: application/json
    status_code: [200, 201]
  delegate_to: localhost
  when: screen_id is defined
