#!/usr/bin/env ruby
require 'fileutils'

require File.dirname(__FILE__) + '/../config/environment.rb'
require 'json'

def create_rep(rep,file)
  if Provider.find_by(string_key: rep['string_key']).nil?
    puts "#{Time.now} - json file #{File.basename(file)} : Error while creating offer, provider with string_key #{rep['string_key']} not found."
    FileUtils.mv(file, "#{Rails.root}/public/features/error/")
  else
    reportage = Reportage.new(no_reportage: rep['no_reportage'], string_key: rep['string_key'], prem_photo: rep['prem_photo'],
                              rep_date: rep['rep_date'], created_at: rep['created_at'], updated_at: rep['updated_at'],
                              rep_titre: rep['rep_titre'], rep_texte: rep['rep_texte'], signatur: rep['signatur'], visible: rep['visible'], offre: 1)
    if reportage.save
      rep['ids'].each do |i|
        ReportagePhoto.create(reportage_id: reportage.id, photo_ms_id: i)
      end
      puts "#{Time.now} - json file #{File.basename(file)} : Offer created"
      File.delete(file)
    else
      puts "#{Time.now} - json file #{File.basename(file)} :  Error while creating offer, #{reportage.errors.full_messages}"
      FileUtils.mv(file, "#{Rails.root}/public/features/error/")
    end
  end
end

def update_rep(rep,reportage,file)
  if reportage.update(nb_photos: rep['nb_photo'], prem_photo: rep['prem_photo'], rep_date: rep['rep_date'],
                      updated_at: rep['updated_at'], rep_titre: rep['rep_titre'], rep_texte: rep['rep_texte'],
                      signatur: rep['signatur'], visible: rep['visible'], offre: 1)
    all_reportage_pics = reportage.reportage_photos.pluck(:photo_ms_id)
    pics_to_remove = all_reportage_pics - rep['ids']
    ReportagePhoto.where(reportage_id: reportage.id, photo_ms_id: pics_to_remove).destroy_all unless pics_to_remove.blank?
    rep['ids'].each_with_index {|i,index| ReportagePhoto.where(reportage_id: reportage.id, photo_ms_id: i).update_all(rang: index)}
    puts "#{Time.now} - json file #{File.basename(file)} : Offer updated"
    File.delete(file)
  else
    puts "#{Time.now} - json file #{File.basename(file)} :  Error while updating offer, #{reportage.errors.full_messages}"
    FileUtils.mv(file, "#{Rails.root}/public/features/error/")
  end
end


FileUtils::makedirs("#{Rails.root}/public/features/error/") unless Dir.exists?("#{Rails.root}/public/features/error/")

# create offer
files = Dir.glob("#{Rails.root}/public/features/create_feature*.json")
files.each do |file|
  rep = JSON.parse open(file).read
  reportage = Reportage.where(no_reportage: rep['no_reportage'], string_key: rep['string_key']).first
  if reportage.nil?
    create_rep(rep,file)
  else
    puts "#{Time.now} - json file #{File.basename(file)} - WARNING : reportage with no_reportage #{rep['no_reportage']} and string_key: #{rep['string_key']} already exists, it will be updated."
    update_rep(rep,reportage,file)
  end
end

# update offer
files = Dir.glob("#{Rails.root}/public/features/update_feature*.json")
files.each do |file|
  rep = JSON.parse open(file).read
  reportage = Reportage.where(no_reportage: rep['no_reportage'], string_key: rep['string_key']).first
  if rep['ids'].blank?
    reportage.destroy
    puts "#{Time.now} - json file #{File.basename(file)} : WARNING : reportage contains no picture, it has been destroyed."
  else
    if reportage.nil?
      puts "#{Time.now} - json file #{File.basename(file)} - WARNING : reportage with no_reportage #{rep['no_reportage']} and string_key: #{rep['string_key']} not found, it will be created."
      create_rep(rep,file)
    else
      update_rep(rep,reportage,file)
    end
  end
end

# destroy offer
files = Dir.glob("#{Rails.root}/public/features/destroy_feature*.json")
files.each do |file|
  rep = JSON.parse open(file).read
  reportage = Reportage.where(no_reportage: rep['no_reportage'], string_key: rep['string_key']).first
  if reportage.nil?
    puts "#{Time.now} - json file #{File.basename(file)} : Error while destroying feature, Reportage '#{rep['no_reportage']}' of agency #{rep['string_key']} doesn't exist."
  else
    reportage.destroy
    puts "#{Time.now} - json file #{File.basename(file)} : Reportage destroyed"
  end
  File.delete(file)
end