<?php

namespace Database\Seeders;

use App\Photo;
use Illuminate\Database\Seeder;

class CopyPhoto extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $path = public_path('uploads/files/aws/');

        $photos = Photo::all();
        foreach ($photos as $item) {
            if ($item->rs_status == 0) continue;
            $fileUrl = $item->file_url;
            if (strpos($fileUrl, "brand/cropped/women/") !== false) {
                $convertedFile = str_replace("brand/cropped/women/", "brand/cropped/women_converted/", $fileUrl);
                $convertedFile = str_replace(".jpeg", ".png", $convertedFile);

                if (file_exists($path . $convertedFile)) {
                    $newFile = str_replace(".jpeg", ".png", $fileUrl);
                    copy($path . $convertedFile, $path . $newFile);

                    $mixFile = str_replace(".png", "_mix.png", $newFile);
                    $oldMixFile = str_replace(".png", "_mix.png", $convertedFile);
                    copy($path . $oldMixFile, $path . $mixFile);

                    $item->update(['file_color' => $newFile, 'file_mix' => $mixFile]);
                    print("Found: " . $item->file_url . "\r\n");
                }
            }
        }
    }
}
