<?php

use Illuminate\Database\Seeder;
use App\Category;
use App\CategoryLocale;
use App\Product;
use App\ProductLocale;
use App\ProductDetail;
use App\ProductQuantity;
use Faker\Factory;
use App\History;
class ProductTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    protected $images;

    public function __construct() {
        $options = [
            'client_id' => 'c646c5ff224124c02f9e291daec43b739cf738fe8383ee5fb255f830c31c4fed',
            'query' => 'food',
            'per_page' => 30
        ];
        $image = Curl::to('https://api.unsplash.com/search/photos?'.http_build_query($options))->get();
        $this->images = json_decode($image);
    }

    public function takeRandom() {
        return $this->images->results[array_rand($this->images->results)];
    }

    public function generateRandomPhoto($path, $orientation='landscape') {
      $result = $this->takeRandom();

      $path = storage_path('app/public/img/'.$path);
      File::makeDirectory($path, 0775, true, true);
      $name = uniqid().'.jpg';
      Image::make($result->urls->regular)->encode('jpg', 95)->save($path.'big_'.$name);
      Image::make($result->urls->regular)->encode('jpg', 95)
        ->resize(null, 450, function ($constraint) {
          $constraint->aspectRatio();
        })
        ->save($path.$name);

      return $name;
    }

    public function run()
    {
    	$faker = Factory::create();

        foreach (range(1, 5) as $i) {
            $category = Category::create([
            	'category_image1' => $this->generateRandomPhoto('products/category/'),
            	'category_image2' => $this->generateRandomPhoto('products/category/', 'squarish'),
        		'category_name:id' => $faker->words(rand(1, 3), true),
        		'category_alt:id' => $faker->sentence(),
                'category_name:en' => $faker->words(rand(1, 3), true),
                'category_alt:en' => $faker->sentence(),
            ]);

            History::create([
                'user_id' => 1,
                'operation' => 'Add Category - '.$faker->words(rand(1, 3), true),
            ]);

            foreach (range(1, 3) as $prod) {
                $code = str_random(5);
                $sort = $prod;
                $product = Product::create([
                    'product_code' => $code,
                    'product_name:id' => $faker->words(rand(1,3), true),
                    'product_content:id' => '<p>'.$faker->paragraphs(rand(1, 3), true).'</p>',
                    'product_name:en' => $faker->words(rand(1,3), true),
                    'product_content:en' => '<p>'.$faker->paragraphs(rand(1, 3), true).'</p>',
                    'product_currency' => '-',
                    'product_cost' => rand(70000, 85000),
                    'product_price' => rand(90000, 100000),
                    'product_stock' => rand(500, 3000),
                    'product_weight' => rand(100, 1400),
                    'product_min_quantity' => rand(100, 200),
                    'production_time' => rand(1, 10),
                    'product_category' => $category->id,
                    'product_display' => 'no',
                    'product_image' => $this->generateRandomPhoto('products/'.$code.'/', 'squarish'),
                    'is_featured' => rand(0,1),
                    'sort' => $sort
                ]);

                History::create([
                    'user_id' => 1,
                    'operation' => 'Add Product - '.$faker->words(rand(1,3), true),
                ]);

                foreach (range(1, 3) as $ia) {
                    $name = $this->generateRandomPhoto('products/'.$code.'/', 'portrait');
                    ProductDetail::create([
                        'detail_code' => $code,
                        'detail_image' => $name,
                        'detail_image_big' => 'big_'.$name,
                    ]);
                }

                foreach (range(1, 10) as $i) {
                    $date = $faker->dateTimeBetween($startDate = '-20 days', $endDate = 'now', $timezone = null);
                    $rand = rand(-50, -5);
                    if ($product->product_stock + $rand > 0) {
                        $sold = ProductQuantity::create([
                            'product_id' => $product->id,
                            'quantity' => $rand,
                            'reason' => $faker->randomElement(['manual', 'online', 'lost']),
                            'is_sales' => true,
                            'created_at' => $date,
                            'updated_at' => $date
                        ]);
                        $product->product_stock = $product->product_stock + $sold->quantity;
                        $product->save();
                        History::create([
                            'user_id' => 1,
                            'operation' => 'Stock down - '.$faker->words(rand(1,3), true).' - '.$faker->words(rand(1,3), true).' ('. $rand .')'
                        ]);
                    }
                }
        	}
        }

    }
}
