<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

/**
 * 导入相册内容到产品表
 * 先在log中复制$tableMapping到这里,然后运行命令
 * php artisan sync:album-content
 */
class SyncAlbumContent extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sync:album-content';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Sync album content to product tables';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        exit;
        $tableMapping = json_decode(' [{"base_product_category":11,"album_path_id":21},{"base_product_category":12,"album_path_id":22},{"base_product_category":13,"album_path_id":31},{"base_product_category":14,"album_path_id":35},{"base_product_category":15,"album_path_id":51},{"base_product_category":16,"album_path_id":64},{"base_product_category":17,"album_path_id":74},{"base_product_category":18,"album_path_id":79},{"base_product_category":19,"album_path_id":83},{"base_product_category":20,"album_path_id":114},{"base_product_category":21,"album_path_id":123},{"base_product_category":22,"album_path_id":141},{"base_product_category":23,"album_path_id":142},{"base_product_category":24,"album_path_id":143},{"base_product_category":25,"album_path_id":145},{"base_product_category":26,"album_path_id":151},{"base_product_category":27,"album_path_id":153},{"base_product_category":28,"album_path_id":154}]  ');

        foreach ($tableMapping as $mapping) {
            $mapping = (array)$mapping;
            $albumContents = DB::table('album_content')
                ->where('path_id', $mapping['album_path_id'])
                ->get();

            foreach ($albumContents as $albumContent) {
                // Insert or update base_product
                $baseProduct = DB::table('base_product')
                    ->where('title', $albumContent->model)
                    ->first();

                $detail = json_decode($albumContent->detail);
                $detail_cn = json_decode($albumContent->detail_cn);

                $content = "";
                if ($detail) {
                    foreach ($detail as $key => $value) {
                        $content .= '<img src="'. $value. '" width="100%">';
                    }
                }
//                if ($detail_cn) {
//                    foreach ($detail_cn as $key => $value) {
//                        $content .= '<img src="'. $value. '" width="100%">';
//                    }
//                }

                $baseProductId = 0;
                $sku = '';
                if (strpos($albumContent->model, "MTB-") === 0) {
                    $sku = $albumContent->model;
                }
                if (!$baseProduct) {
                    $baseProduct = DB::table('base_product')
                        ->insertGetId([
                            'title' => $albumContent->model,
                            'category_id' => $mapping['base_product_category'],
                            'sku' => $sku,
                            'content' => $content,
                            'created_at' => Carbon::now(),
                            'updated_at' => Carbon::now(),
                            'seo_title' => $albumContent->model,
                        ]);
                    $baseProductId = $baseProduct;
                } else {
                    DB::table('base_product')
                        ->where('id', $baseProduct->id)
                        ->update([
                            'title' => $albumContent->model,
                            'category_id' => $mapping['base_product_category'],
                            'sku' => $sku,
                            'content' => $content,
                            'updated_at' => Carbon::now(),
                            'seo_title' => $albumContent->model,
                        ]);
                    $baseProductId = $baseProduct->id;
                }
//                var_dump($baseProduct->id);
//                exit;

                // Insert base_product_image
                $photos = json_decode($albumContent->photo, true);
                if (is_array($photos)) {
                    foreach ($photos as $photo) {
                        DB::table('base_product_image')
                            ->insert([
                                'product_id' => $baseProductId,
                                'image_url' => $photo,
                                'created_at' => Carbon::now(),
                                'updated_at' => Carbon::now(),
                            ]);
                    }
                }

                // Insert base_video
                $videos = json_decode($albumContent->video, true);
                if (is_array($videos)) {
                    foreach ($videos as $video) {
                        if (isset($video['video_src']) && $video['video_src'] !== '0' && !empty($video['video_src'])) {
                            DB::table('base_video')
                                ->insert([
                                    'title' => $video['video_title'] ?? null,
                                    'category_id' => 1,
                                    'video_url' => $video['video_src'],
                                    'cover_image' => $video['cover'] ?? null,
                                    'enabled' => 1,
                                    'created_at' => Carbon::now(),
                                    'updated_at' => Carbon::now(),
                                ]);
                        }
                    }
                }
            }
        }

        $this->info('Sync completed successfully.');
        return 0;
    }
}