<?php

use App\Fortuneteller;
use App\Http\Resources\Admin\FortunetellerResource;
use App\Shop;
use Illuminate\Database\Seeder;
use Illuminate\Support\Arr;

class ExportTellerNotTokyo extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $listedId = [];
        $areaTokyoId = 13;
        $shopIds = Shop::select('id')->where('area_id', $areaTokyoId)->pluck('id')->toArray();
        $list = Fortuneteller::with('shop')->notDeleted()->whereNotIn('shop_id', $shopIds)->orderBy('shop_id')->orderBy('id')->get();
        $total = 0;
        $csvData = '"ID", "Fortune Teller", "Area", "Shop", "Shop Status", "Teller Status"' . "\r\n";
        $csvContent = "";
        foreach ($list as $item) {
            $shopName = (isset($item->shop) && $item->shop != null) ? $item->shop->name : "";
            $areaName = (isset($item->shop->area) && $item->shop->area != null) ? $item->shop->area->name : "";
            $shopActivated = (isset($item->shop) && $item->shop != null && $item->shop->is_activated == 1) ? "Published" : "Draft";
            $activated = ($item->is_activated == 1) ? "Published" : "Draft";
            $csvContent .= '"' . $item->id . '", "' . $item->name . '", "' . $areaName . '", "' . $shopName . '", "' . $shopActivated . '", "' . $activated . '"' . "\r\n";
            $total += 1;
            if (isset($item->shop) && $item->shop->id != null) $listedId[] = $item->shop->id;
        }
        $csvData .= $csvContent;
        $fileName = "teller_not_tokyo_" . date('YmdHis') . ".csv";
        file_put_contents(public_path("uploads/csv/" . $fileName), $csvData);
        Shop::whereIn('id', $listedId)->update(['area_id' => 13]);
        print "Exported: " . $fileName . " --- " . $total . " items. \r\n";

        /*$total = Fortuneteller::where('is_deleted', false)->count();
        $tokyo = Shop::select('id')->where('area_id', 13)->pluck('id')->toArray();
        $tokyoList = Fortuneteller::where('is_deleted', false)->whereIn('shop_id', $tokyo)->count();
        print "Total: " . $total . "\r\n";
        print "Tokyo: 436 - " . $tokyoList . "\r\n";

        $shopsIgnoreIds = Shop::select('id')->where('area_id', $areaTokyoId)->pluck('id')->toArray();
        $all = Fortuneteller::where('is_deleted', false)->whereNotIn('shop_id', $shopsIgnoreIds)->get();
        foreach ($all as $key => $it) {
            if (!in_array($it->id, $listedId)) {
                print "Not In list: " . ($key + 1) . " - " . $it->id . "\r\n";
            }
        }*/
    }
}
