<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\RelationArticlePhoto;
use Illuminate\Support\Facades\Storage;

class RelationArticlePhotoController extends Controller
{
    /**
     * 施設検索条件設定画面の表示
     * @return string
     */
    public function changePropertyImage()
    {
//      ②property_sub of MNSION house photo = 「0」 please replace it with「4」（現地外観写真）
//      ③property_sub of another house (other than MANSION) = 「0」please replace it with「1」

        if (auth()->user()->company_id === 3) {
            $isMansionArticle = [6, 7];

//            RelationArticlePhoto::query()
//                ->where(function ($q) {
//                    $q->where('property_sub', 0);
//                    $q->orWhere('property_sub', null);
//                })
//                ->whereIn('article_id', function ($query) use ($isMansionArticle) {
//                    $query->select('building_id')
//                        ->from('articles')
//                        ->whereIn('property', $isMansionArticle);
//                })->update(['property_sub' => 4]);
//
//            RelationArticlePhoto::query()
//                ->where(function ($q) {
//                    $q->where('property_sub', 0);
//                    $q->orWhere('property_sub', null);
//                })
//                ->whereNotIn('article_id', function ($query) use ($isMansionArticle) {
//                    $query->select('building_id')
//                        ->from('articles')
//                        ->whereIn('property', $isMansionArticle);
//                })->update(['property_sub' => 1]);
//
//
//            $a = RelationArticlePhoto::query()
//                ->where(function ($q) {
//                    $q->where('property_sub', 0);
//                    $q->orWhere('property_sub', null);
//                })->get();

            // copy mansion img csv to other folder
            $listPathImgToCopy = RelationArticlePhoto::query()
                ->where('property_sub', 4)
                ->where('file_path', 'LIKE', '%/storage/company_3/img-csv%')
                ->whereIn('article_id', function ($query) use ($isMansionArticle) {
                    $query->select('building_id')
                        ->from('articles')
                        ->whereIn('property', $isMansionArticle);
                })
                ->get()->pluck('file_path')->toArray();

            foreach ($listPathImgToCopy as $path) {
                $sourcePath = str_replace('/storage/', 'public/', $path);
                $destinationPath = str_replace('/storage/company_3/img-csv/', 'public/company_3/copy-img-csv/', $path);
                if (!Storage::exists($destinationPath) && Storage::exists($sourcePath)) {
                    Storage::copy($sourcePath, $destinationPath);
                }
            }

            dd($listPathImgToCopy);
        }
    }
}
