<?php

namespace Database\Seeders;

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

class ImportAIPhoto extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $basePath = public_path("uploads/files/aws/");
        Photo::truncate();
        $count = 0;

        $csvFile = public_path("uploads/data/20220512_tab_1.csv");
        $file = fopen($csvFile, "a+");
        while (!feof($file)) {
            $line = fgetcsv($file);
            if (!$line || $line == null || count($line) <= 0 || $line[1] != "チェック完了") continue;
            $fileUrl = $line[4];
            $fileUrl = str_replace("https://s3.console.aws.amazon.com/s3/object/facetryon-ml-source?region=ap-northeast-1&prefix=", "", $fileUrl);
            $fileStatus = 0;
            if (file_exists($basePath . $fileUrl)) $fileStatus = 1;

            Photo::create([
                'code'        => $line[0],
                'status'      => $line[1],
                'author'      => $line[2],
                'category'    => $line[3],
                'aws_path'    => $line[4],
                'file_url'    => $fileUrl,
                'file_origin' => basename($fileUrl),
                'file_level'  => 0,
                'user_id'     => 1,
                'rs_status'   => $fileStatus,
                'created_at'  => date('Y-m-d H:i:s'),
                'updated_at'  => date('Y-m-d H:i:s'),
            ]);

            $count++;
            print($count . ". Imported: " . $line[4] . "\r\n");
        }


        $csvFile = public_path("uploads/data/20220512_tab_2.csv");
        $file = fopen($csvFile, "a+");
        while (!feof($file)) {
            $line = fgetcsv($file);
            if (!$line || $line == null || count($line) <= 0 || $line[1] != "チェック完了") continue;
            $fileUrl = $line[3];
            $fileUrl = str_replace("https://s3.console.aws.amazon.com/s3/object/facetryon-ml-source?region=ap-northeast-1&prefix=", "", $fileUrl);
            $fileStatus = 0;
            if (file_exists($basePath . $fileUrl)) $fileStatus = 1;

            Photo::create([
                'code'        => $line[0],
                'status'      => $line[1],
                'author'      => $line[2],
                'category'    => 'stylist',
                'aws_path'    => $line[3],
                'file_url'    => $fileUrl,
                'file_origin' => basename($fileUrl),
                'file_level'  => 0,
                'user_id'     => 1,
                'rs_status'   => $fileStatus,
                'created_at'  => date('Y-m-d H:i:s'),
                'updated_at'  => date('Y-m-d H:i:s'),
            ]);

            $count++;
            print($count . ". Imported: " . $line[3] . "\r\n");
        }


        $csvFile = public_path("uploads/data/20220512_tab_3.csv");
        $file = fopen($csvFile, "a+");
        while (!feof($file)) {
            $line = fgetcsv($file);
            if (!$line || $line == null || count($line) <= 0 || $line[1] != "チェック完了") continue;
            $fileUrl = $line[4];
            $fileUrl = str_replace("https://s3.console.aws.amazon.com/s3/object/facetryon-ml-source?region=ap-northeast-1&prefix=", "", $fileUrl);
            $fileStatus = 0;
            if (file_exists($basePath . $fileUrl)) $fileStatus = 1;

            Photo::create([
                'code'        => $line[0],
                'status'      => $line[1],
                'author'      => $line[2],
                'category'    => $line[3],
                'aws_path'    => $line[4],
                'file_url'    => $fileUrl,
                'file_origin' => basename($fileUrl),
                'file_level'  => 0,
                'user_id'     => 1,
                'rs_status'   => $fileStatus,
                'created_at'  => date('Y-m-d H:i:s'),
                'updated_at'  => date('Y-m-d H:i:s'),
            ]);

            $count++;
            print($count . ". Imported: " . $line[4] . "\r\n");
        }
    }
}
