<?php

use Illuminate\Database\Seeder;
use App\Area;
use App\Shop;
use App\ShopMeta;
use App\Fortuneteller;
use App\Fortunemethod;
use App\FortunemethodDetail;
use App\Consultation;
use App\ConsultationDetail;

class CheckAreaDuplicate extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //Checking City
        print "---------------------------------------- Checking City ----------------------------------------\r\n";
        $areaIds = Area::select('id')->where('is_deleted', false)->orderBy('id')->pluck('id')->toArray();
        foreach ($areaIds as $id) {
            $area = Area::where('id', $id)->first();
            if (isset($area) && $area->id != null) {
                $areas = Area::where('is_deleted', false)->where('id', '!=', $id)->orderBy('id')->get();
                foreach ($areas as $item) {
                    $areaName = trim($area->name) . "都";
                    $areaName = str_replace("都都", "都", $areaName);

                    $areaName2 = trim($area->name) . "2222";
                    $areaName2 = str_replace("都2222", "", $areaName2);
                    $areaName2 = str_replace("2222", "", $areaName2);

                    if (trim($area->name) == trim($item->name) || $areaName == trim($item->name) || $areaName2 == trim($item->name)) {
                        print "----------------------------------------\r\nCheck City item: " . $area->name . " --- " . $area->id . "\r\n";
                        print "Found Duplicate Item: " . $item->name . " --- " . $item->id . "\r\n";
                        Area::where('parent_id', $item->id)->update(['parent_id' => $area->id]);
                        Shop::where('area_id', $item->id)->update(['area_id' => $area->id]);
                        Area::where('id', $item->id)->delete();
                    }
                }
            }
        }

        //Checking District
        print "---------------------------------------- Checking District ----------------------------------------\r\n";
        $areaIds = Area::select('id')->where('is_deleted', false)->orderBy('id')->pluck('id')->toArray();
        foreach ($areaIds as $id) {
            $area = Area::where('id', $id)->first();
            if (isset($area) && $area->id != null) {
                $areas = Area::where('is_deleted', false)->where('id', '!=', $id)->orderBy('id')->get();
                foreach ($areas as $item) {
                    $areaName = trim($area->name) . "県";
                    $areaName = str_replace("県県", "県", $areaName);

                    $areaName2 = trim($area->name) . "2222";
                    $areaName2 = str_replace("県2222", "", $areaName2);
                    $areaName2 = str_replace("2222", "", $areaName2);

                    if (trim($area->name) == trim($item->name) || $areaName == trim($item->name) || $areaName2 == trim($item->name)) {
                        print "----------------------------------------\r\nCheck District item: " . $area->name . " --- " . $area->id . "\r\n";
                        print "Found Duplicate Item: " . $item->name . " --- " . $item->id . "\r\n";
                        Area::where('parent_id', $item->id)->update(['parent_id' => $area->id]);
                        Shop::where('area_id', $item->id)->update(['area_id' => $area->id]);
                        Area::where('id', $item->id)->delete();
                    }
                }
            }
        }

        //Checking all
        print "---------------------------------------- Checking Area ----------------------------------------\r\n";
        $areaIds = Area::select('id')->where('is_deleted', false)->orderBy('id')->pluck('id')->toArray();
        foreach ($areaIds as $id) {
            $area = Area::where('id', $id)->first();
            if (isset($area) && $area->id != null) {
                $areas = Area::where('is_deleted', false)->where('id', '!=', $id)->orderBy('id')->get();
                foreach ($areas as $item) {
                    if (trim($area->name) == trim($item->name)) {
                        print "----------------------------------------\r\nCheck item: " . $area->name . " --- " . $area->id . "\r\n";
                        print "Found Duplicate Item: " . $item->name . " --- " . $item->id . "\r\n";
                        Area::where('parent_id', $item->id)->update(['parent_id' => $area->id]);
                        Shop::where('area_id', $item->id)->update(['area_id' => $area->id]);
                        Area::where('id', $item->id)->delete();
                    }
                }
            }
        }

        //Shop
        print "---------------------------------------- Checking Shop ----------------------------------------\r\n";
        $checkedShop = [];
        $shopIds = Shop::select('id')->where('is_deleted', false)->orderBy('id')->pluck('id')->toArray();
        foreach ($shopIds as $id) {
            $shop = Shop::where('id', $id)->first();
            if (isset($shop) && $shop->id != null) {
                $shops = Shop::where('is_deleted', false)->where('id', '!=', $id)->whereNotIn('id', $checkedShop)->orderBy('id')->get();
                foreach ($shops as $item) {
                    $name1 = trim($shop->name);
                    $name2 = trim($item->name);

                    if ($name1 == $name2 && $shop->area_id == $item->area_id) {
                        $checkInfoDuplicate = true;
                        foreach ($shop->information as $i) {
                            foreach ($item->information as $j) {
                                if ($i->title !== null && trim($i->title) !== "" && trim($i->title) == trim($j->title)) {
                                    if (trim($i->content) != trim($j->content)) {
                                        $checkInfoDuplicate = false;
                                    }
                                }
                            }
                        }
                        if ($checkInfoDuplicate) {
                            print "----------------------------------------\r\n";
                            print "Check Shop item: " . $shop->area_id . " --- " . str_pad($shop->id, 8) . " --- " . $shop->name ."\r\n";
                            print "Found Shop item: " . $item->area_id . " --- " . str_pad($item->id, 8) . " --- " . $item->name ."\r\n";
                            Fortuneteller::where('shop_id', $item->id)->update(['shop_id' => $shop->id]);
                            ShopMeta::where('shop_id', $item->id)->delete();
                            Shop::where('id', $item->id)->delete();
                        }
                    }
                }
                $checkedShop[] = $id;
            }
        }

        //Method
        print "---------------------------------------- Checking Method ----------------------------------------\r\n";
        $checkedMethod = [];
        $methodIds = Fortunemethod::select('id')->where('is_deleted', false)->orderBy('id')->pluck('id')->toArray();
        foreach ($methodIds as $id) {
            $method = Fortunemethod::where('id', $id)->first();
            if (isset($method) && $method->id != null) {
                $methods = Fortunemethod::where('is_deleted', false)->where('id', '!=', $id)->whereNotIn('id', $checkedMethod)->orderBy('id')->get();
                foreach ($methods as $item) {
                    $name1 = trim($method->name);
                    $name2 = trim($item->name);
                    if ($name1 == $name2) {
                        print "----------------------------------------\r\n";
                        print "Check Method item: " . str_pad($method->id, 8) . " --- " . $method->name ."\r\n";
                        print "Found Method item: " . str_pad($item->id, 8) . " --- " . $item->name ."\r\n";
                        FortunemethodDetail::where('fortune_method_id', $item->id)->update(['fortune_method_id' => $method->id]);
                        Fortunemethod::where('id', $item->id)->delete();
                    }
                }
                $checkedMethod[] = $id;
            }
        }

        $checkedMethodDetail = [];
        $methodIds = FortunemethodDetail::select('id')->orderBy('id')->pluck('id')->toArray();
        foreach ($methodIds as $id) {
            $method = FortunemethodDetail::where('id', $id)->first();
            if (isset($method) && $method->id != null) {
                $methods = FortunemethodDetail::where('id', '!=', $id)->whereNotIn('id', $checkedMethodDetail)->orderBy('id')->get();
                foreach ($methods as $item) {
                    $tellerId1 = trim($method->fortune_teller_id);
                    $methodId1 = trim($method->fortune_method_id);
                    $tellerId2 = trim($item->fortune_teller_id);
                    $methodId2 = trim($item->fortune_method_id);
                    if ($tellerId1 == $tellerId2 && $methodId1 == $methodId2) {
                        FortunemethodDetail::where('id', $item->id)->delete();
                    }
                }
                $checkedMethodDetail[] = $id;
            }
        }

        print "---------------------------------------- Checking Consultation ----------------------------------------\r\n";
        $checkedConsultation = [];
        $consultationIds = Consultation::select('id')->where('is_deleted', false)->orderBy('id')->pluck('id')->toArray();
        foreach ($consultationIds as $id) {
            $consultation = Consultation::where('id', $id)->first();
            if (isset($consultation) && $consultation->id != null) {
                $consultations = Consultation::where('is_deleted', false)->where('id', '!=', $id)->whereNotIn('id', $checkedConsultation)->orderBy('id')->get();
                foreach ($consultations as $item) {
                    $name1 = trim($consultation->name);
                    $name2 = trim($item->name);
                    if ($name1 == $name2) {
                        print "----------------------------------------\r\n";
                        print "Check Consultation item: " . str_pad($consultation->id, 8) . " --- " . $consultation->name ."\r\n";
                        print "Found Consultation item: " . str_pad($item->id, 8) . " --- " . $item->name ."\r\n";
                        ConsultationDetail::where('consultation_id', $item->id)->update(['consultation_id' => $consultation->id]);
                        Consultation::where('id', $item->id)->delete();
                    }
                }
                $checkedConsultation[] = $id;
            }
        }
        $checkedConsultationDetail = [];
        $consultationIds = ConsultationDetail::select('id')->orderBy('id')->pluck('id')->toArray();
        foreach ($consultationIds as $id) {
            $consultation = ConsultationDetail::where('id', $id)->first();
            if (isset($consultation) && $consultation->id != null) {
                $consultations = ConsultationDetail::where('id', '!=', $id)->whereNotIn('id', $checkedConsultationDetail)->orderBy('id')->get();
                foreach ($consultations as $item) {
                    $tellerId1 = trim($consultation->fortune_teller_id);
                    $consultationId1 = trim($consultation->consultation_id);
                    $tellerId2 = trim($item->fortune_teller_id);
                    $consultationId2 = trim($item->consultation_id);
                    if ($tellerId1 == $tellerId2 && $consultationId1 == $consultationId2) {
                        ConsultationDetail::where('id', $item->id)->delete();
                    }
                }
                $checkedConsultationDetail[] = $id;
            }
        }

    }
}
