services:
  find-remove-duplicates:
      command:
      - /bin/sh
      - '-lc'
      - |-
          python - <<'PY'
          import os
          import csv
          import hashlib
          from collections import defaultdict

          ROOT = "/scan"
          OUT = "/scan/duplicates.csv"

          def sha256_file(path, chunk_size=1024 * 1024):
              h = hashlib.sha256()
              with open(path, "rb") as f:
                  while True:
                      chunk = f.read(chunk_size)
                      if not chunk:
                          break
                      h.update(chunk)
              return h.hexdigest()

          size_groups = defaultdict(list)
          total_files_scanned = 0

          # Step 1: group by size
          for root, dirs, files in os.walk(ROOT):
              for name in files:
                  path = os.path.join(root, name)

                  # avoid hashing/deleting the report itself
                  if path == OUT:
                      continue

                  try:
                      st = os.stat(path, follow_symlinks=False)
                  except Exception:
                      continue

                  if os.path.islink(path):
                      continue

                  total_files_scanned += 1
                  size_groups[st.st_size].append(path)

          # Step 2: hash only files with duplicate size
          hash_groups = defaultdict(list)
          file_sizes = {}

          for size, paths in size_groups.items():
              if len(paths) < 2:
                  continue
              for path in paths:
                  try:
                      digest = sha256_file(path)
                      hash_groups[digest].append(path)
                      file_sizes[path] = size
                  except Exception:
                      pass

          # Step 3: remove duplicates, keep first path in sorted order
          removed_files = 0
          removed_bytes = 0
          group_id = 1
          csv_rows = []

          for digest, paths in hash_groups.items():
              if len(paths) < 2:
                  continue

              paths = sorted(paths)
              keep = paths[0]
              duplicates = paths[1:]

              csv_rows.append([group_id, keep, digest, "kept"])

              for dup in duplicates:
                  try:
                      size = file_sizes.get(dup, 0)
                      os.remove(dup)
                      removed_files += 1
                      removed_bytes += size
                      csv_rows.append([group_id, dup, digest, "deleted"])
                  except Exception as e:
                      csv_rows.append([group_id, dup, digest, f"delete_failed: {e}"])

              group_id += 1

          # Step 4: write CSV report
          with open(OUT, "w", newline="", encoding="utf-8") as f:
              w = csv.writer(f)
              w.writerow(["group_id", "path", "hash", "action"])
              w.writerows(csv_rows)

          print(f"CSV report written to: {OUT}")
          print(f"Total files scanned: {total_files_scanned}")
          print(f"Duplicate files removed: {removed_files}")
          print(f"Reclaimed bytes: {removed_bytes}")
          print(f"Reclaimed MiB: {removed_bytes / (1024 * 1024):.2f}")
          PY
      image: python:3.12-alpine
      restart: 'no'
      user: '3001:3001'
      volumes:
      - /mnt/Vault/users/UserA/Dir:/scan