#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../config/environment.rb'

# check all PixTech stocks pictures to find orphan ones : the ones that don't belong to author

stocks_prov = Provider.where('provider_type_id=1 and pixtech_id=1')
stocks_prov.each do |stock|
  #stock_imgs = Image.joins(:providers).where(provider: stock)
  stock_imgs = stock.images.where(content_error: 0)
  puts "#{stock.name} : #{stock_imgs.count} images"
  pictures_list = "#{Time.now.strftime('%Y-%m-%d-%Hh%Mm%S')}_#{stock.string_key}_orphan_stock_pictures.txt"
  nb_orphans = 0
  File.open(pictures_list,'wb') do |f|
    stock_imgs.each do |img|
      img_dups = Image.where("original_filename = \"#{img.original_filename}\" and file_name != \"#{img.file_name}\"")
      if img_dups.count == 0
        f.write("#{img.file_name}\n")
        nb_orphans += 1
      end
    end
  end
  puts "#{nb_orphans} images orphelines"
end
