<?php

namespace App\Http\Controllers\Admin;

use App\Models\Article;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Laracsv\Export as LaracsvExport;

class ExportController extends Controller
{
    private $filename = "bukken.csv";

    public function export(Request $request)
    {
        $columns = [
            "comment" => "広告補足情報(自社Web)(20)", "point" => "広告補足情報(自社Web)(19)", "property" => "物件分類名",
            "history" => "価格変更履歴　自社データー表示(物件備考内容)", "E" => "過去のリフォーム内容②(物件備考内容)",
            "custom_field_5" => "過去のリフォーム内容①(物件備考内容)", "G" => "過去のリフォームの有無(物件備考内容)",
            "H" => "販売時のリフォーム内容②(物件備考内容)", "I" => "販売時のリフォーム内容①(物件備考内容)",
            "J" => "販売時のリフォームの有無(物件備考内容)", "K" => "一覧の行番号", "price" => "売出価格(万円)(1)",
            "management_company" => "管理業者名称", "gas" => "ガス", "sewerage" => "排水", "water_supply" => "水道", "company_manner" => "取引態様",
            "management_form1" => "管理形態(管理方法)", "management_form2" => "管理形態(管理人)", "T" => "建物の延床坪数", "U" => "建物の延床面積",
            "balcony_area_val" => "ﾊﾞﾙｺﾆｰ面積", "W" => "専有坪数", "total_area_val" => "専有面積", "Y" => "土地坪数", "land_area_val" => "土地面積",
            "total_area" => "専有面積計測方式", "total_unit" => "総戸数", "build_year" => "築年月数", "AD" => "築年数(月切上げ)",
            "AE" => "築年数(月切捨て)", "AF" => "竣工日", "architecture_no" => "建築確認番号", "property_sub" => "新築区分",
            "AI" => "構造＋階建て", "custom_field_6" => "駐車場備考", "parking" => "駐車場有無", "balcony_direction" => "向き",
            "AM" => "階部分", "floor_plan" => "間取り", "AO" => "接道状況④", "AP" => "接道状況③", "AQ" => "接道状況②", "AR" => "接道状況①",
            "land_right" => "土地権利", "road_burden_area" => "私道負担面積", "road_burden" => "私道負担", "has_city_plan" => "建築条件",
            "kokudoho" => "国土法", "terrain" => "地勢", "city_plan" => "都市計画", "AZ" => "法令制限②", "other_comment" => "法令制限①",
            "section" => "総区画数", "volume_rate" => "容積率", "building_rate" => "建ぺい率", "land_use" => "用途地域②",
            "use_area" => "用途地域①", "ground" => "地目", "BH" => "交通その他②", "BI" => "交通その他①",
            "BJ" => "交通(6)", "BK" => "交通(5)", "BL" => "交通(4)", "BM" => "交通(3)",
            "BN" => "交通(2)", "BO" => "交通(1)", "BP" => "沿線名+駅名(6)", "BQ" => "沿線名+駅名(5)",
            "BR" => "沿線名+駅名(4)", "BS" => "沿線名+駅名(3)", "BT" => "沿線名+駅名(2)", "BU" => "沿線名+駅名(1)",
            "BV" => "徒歩時間(6)", "BW" => "徒歩時間(5)", "BX" => "徒歩時間(4)", "BY" => "徒歩時間(3)",
            "BZ" => "徒歩時間(2)", "CA" => "徒歩時間(1)", "CB" => "自社Web地図表示", "CC" => "地図表示",
            "CD" => "丁番地公開", "CE" => "WMP PC・検索ｴﾝｼﾞﾝ送信有無", "members" => "会員専用有無", "CG" => "送信有無",
            "repair_cost" => "修繕積立金", "management_cost" => "管理費", "point_recommend" => "セールスポイント", "move_in_conditions" => "引渡条件",
            "move_in" => "引渡日", "secondary_school_school" => "中学校区", "primary_school_school" => "小学校区", "place_area" => "所在地区分",
            "lng" => "経度", "lat" => "緯度", "CR" => "丁番地(物件)", "address3" => "町地域名(物件)",
            "address2" => "市区町村名(物件)", "current_status" => "現況", "CV" => "売止日", "CW" => "成約日",
            "created_at1" => "登録・更新日", "created_at2" => "情報登録日", "CZ" => "物件区分(売出･売止･成約)", "DA" => "自社・他社区分",
            "room_num" => "部屋番号", "name" => "物件名称", "building_id" => "物件№"
        ];

        $articles = Article::whereNotIn("status", [4,5])
            ->whereIn("address1", [11,12])
            ->where("own_company", 2)
            ->where(function ($query) {
                return $query->whereIn("property", [1,2,3,4,5])
                    ->orWhereNull("property");
            })
            ->where(function ($query) {
                return $query->whereNull("del")
                    ->orWhere('del', '0');
            })
            ->get();

        $csvExporter = new LaracsvExport();

        $i = 1;

        $csvExporter->beforeEach(function ($article) use (&$i) {
            $mansion = $article->getMansion();
            if ($mansion) {
                $article->created_at = $mansion->created_at;
                $article->construction = $mansion->construction;
                $article->total_unit = $mansion->total_unit;
                $article->age_year = $mansion->age_year;
                $article->age_month = $mansion->age_month;
                $article->address4 = $mansion->address4;
                $article->land_area_val = $mansion->land_area_val;
                $article->sewerage = $mansion->sewerage;
                $article->management_company = $mansion->management_company;
                $article->city_plan = $mansion->city_plan;
                $article->management_form = $mansion->management_form;
            }
            $article->CR = $article->address4;
            if ($article->own_company == 2) {
                $article->CE = "〇";
                $article->CG = "〇";
            }
            $article->property = $this->getPropertyName($article);
            $article->history = $this->getCustomField($article, 7);
            $article->gas = $this->getGas($article);
            $article->sewerage = $this->getSewerage($article);
            $article->water_supply = $this->getWaterSupply($article);
            $article->company_manner = $this->getCompanyManager($article);
            $article->property_sub = $this->getPropertySub($article);
            $article->H = $this->getCustomField($article, 4);
            $article->J = empty($article->H) ? "" : "有";
            $article->I = $this->getCustomField($article, 4);
            $article->custom_field_5 = $this->getCustomField($article, 5);
            $article->custom_field_6 = $this->getCustomField($article, 6);
            if (!empty($article->custom_field_5) && !empty($article->I)) {
                $article->G = "有";
            }
            $article->parking = $this->getParking($article);
            $article->floor_plan = $this->getFloor($article);
            $article->land_right = $this->getLandRight($article);
            $article->road_burden = $this->getRoadBurden($article);
            $article->kokudoho = $this->getKokudoho($article);
            $article->terrain = $this->getTerrain($article);
            $article->city_plan = $this->getCityPlan($article);
            $article->secondary_school_school = $this->getSecondarySchool($article);
            $article->primary_school_school = $this->getPrimarySchool($article);
            $article->address3 = $this->getAddress3($article);
            $article->address2 = $this->getAddress2($article);
            $article->K = "";
            $article->management_form1 = $this->getManagementForm($article);
            $article->management_form2 = $this->getSData($article);
            $article->total_area = $this->getTotalArea($article);
            $article->BO = $this->getStation($article, 1);
            $article->BN = $this->getStation($article, 1, true);
            $article->BM = $this->getStation($article, 2);
            $article->BL = $this->getStation($article, 2, true);
            $article->BK = $this->getStation($article, 3);
            $article->BJ = $this->getStation($article, 3, true);
            $article->BU = $this->getStation($article, 1, false, false);
            $article->BT = $this->getStation($article, 2, false, false);
            $article->BS = $this->getStation($article, 3, false, false);
            $article->CA = $article->main_traffic_time;
            $article->BZ = $article->sub_traffic1_time;
            $article->BY = $article->sub_traffic2_time;
            $article->members = $article->members == 1 ? "〇" : "✕";
            $article->current_status = $this->getCurrentStatus($article);
            $updateTime = $this->convertDateTime($article->conf_day ?? "");
            $article->created_at1 = $updateTime;
            $article->created_at2 = date('m/d/Y',  strtotime($article->created_at));
            $article->CZ = "売出";
            $article->build_year = $this->getAEData($article);
            $article->AI = $this->getAIData($article);
            $article->balcony_direction = $this->getBalconyDirection($article);
            $article->has_city_plan = $this->getHasCity($article);
            $article->use_area = $this->getUseArea($article->use_area);
            $article->land_use = $this->getUseArea($article->land_use);
            $article->AR = $this->getLine($article, 1);
            $article->AQ = $this->getLine($article, 2);
            $article->AP = $this->getLine($article, 3);
            $article->ground = $this->getGround($article);
            $article->move_in = $this->getMoveIn($article);
            $article->W = $article->property == "一戸建て" ? "" : $this->roundFl($article->total_area_val * 0.3025);
            $article->Y = $this->roundFl($article->land_area_val * 0.3025);
            $article->AF = $this->getAFData($article);
            $article->AE = $this->getAEData($article, true);
            $article->AD = $this->getAEData($article, false);
            $article->U = $this->getUData($article);
            $article->T = $this->getTData($article);
            $article->AM = $this->getAMData($article);
            $article->total_area_val = $article->property == "一戸建て" ? "" : $article->total_area_val;
        });

        $content = $csvExporter->build($articles, $columns)->getWriter()->getContent();
        $currentPath = base_path() . "/data/{$this->filename}";
        file_put_contents($currentPath, $content);
        $this->uploadRemoteSai($currentPath);
        return "Export success";
    }

    private function getTData($article)
    {
        $property = $article->property;
        switch ($property) {
            case "土地":
            case "マンション":
                return "";
        }

        return $this->roundFl($article->total_area_val * 0.3025);
    }

    private function convertDateTime($date)
    {
        if (empty($date)) return "";
        try {
            $time = Carbon::parse($date);
        } catch (\Error $error) {
            return "";
        } catch (\Exception $error) {
            return "";
        }
        $month = strlen($time->month) == 1 ? "{$time->month}" : $time->month;
        $day = strlen($time->day) == 1 ? "{$time->day}" : $time->day;

        return "{$month}/{$day}/{$time->year}";
    }

    private function getConstruction($article)
    {
        if ($article->construction == 1) {
            return "木造{$article->floor}階建て";
        }

        $constructions = [
            0 => "未選択",
            1 => "木造",
            2 => "ブロック",
            3 => "鉄骨",
            4 => "RC",
            11 => "RCB",
            5 => "SRC",
            10 => "WRC",
            6 => "PC",
            7 => "HPC",
            8 => "軽量鉄骨",
            9 => "ALC"
        ];

        $construction = $constructions[$article->construction] ?? "";

        return "{$construction}造{$article->floor}階建て";
    }

    private function getHasCity($article)
    {
        $property = $article->property;
        switch ($property) {
            case "土地":
                if ($article->land_condition == 2) {
                    return "有";
                }
            case "一戸建て":
            case "マンション":
                return "";
                break;
        }
        return "";
    }

    private function getAMData($article)
    {
        $property = $article->property;
        switch ($property) {
            case "土地":
            case "一戸建て":
                return "";
                break;
        }

        return $article->whereabouts;
    }

    private function getSData($article)
    {
        $property = $article->property;
        switch ($property) {
            case "土地":
            case "一戸建て":
                return "";
                break;
            case "マンション":
                break;
        }

        $managements = [
            0 => "",
            1 => "常駐",
            2 => "通勤",
            3 => "巡回",
            4 => "勤務形態未定"
        ];

        return $managements[$article->management_form] ?? "-";
    }

    private function getUData($article)
    {
        $property = $article->property;
        switch ($property) {
            case "土地":
            case "マンション":
                return "";
                break;
            case "一戸建て":
                return $article->total_area_val;
                break;
        }

        return "";
    }

    private function getAEData($article, $onlyYear = null)
    {
        $property = $article->property;
        switch ($property) {
            case "土地":
                return "";
                break;
            case "一戸建て":
                $isNewHouse = $article->property_sub == 1;
                if ($isNewHouse) {
                    return "";
                }
                break;
            case "マンション":
                break;
        }
        try {
            $now = Carbon::now();
            $ageYear = trim($article->age_year);
            $ageMonth = trim($article->age_month);
            $dateAge = "{$ageYear}-{$ageMonth}-01";
            $target = Carbon::parse($dateAge);
            $month = $now->diffInMonths($target);
            $yearAge = intval($month / 12);
            $monthAge = $month % 12;
        } catch (\Error $error) {
            return "";
        } catch (\Exception $error) {
            return "";
        }


        if (is_null($onlyYear)) {
            if ($monthAge > 0)
                $yearAge = $yearAge + 1;

            return "{$yearAge}年{$monthAge}ヶ月";
        }

        if ($onlyYear)
            return $yearAge;

        if ($monthAge > 0)
            return $yearAge + 1;

        return $yearAge;
    }

    private function getAFData($article)
    {
        $property = $article->property;
        $ageYear = $article->age_year;
        $ageMonth = $article->age_month;
        $completedYear = $article->age_year;
        $completedMonth = $article->completed_month;
        if (strlen($ageMonth) < 2) {
            $ageMonth = " {$ageMonth}";
        }
        if (strlen($completedMonth) < 2) {
            $completedMonth = " {$completedMonth}";
        }
        switch ($property) {
            case "土地":
                return "";
                break;
            case "一戸建て":
                if ($article->property_sub == 1) {
                       return "{$completedYear}/{$completedMonth}";
                }
                return "{$ageYear}/{$ageMonth}";
                break;
            case "マンション":
                return "{$ageYear}/{$ageMonth}";
                break;
        }

        return "";
    }

    private function getAIData($article)
    {
        $property = $article->property;
        switch ($property) {
            case "土地":
                return "";
                break;
            default:
                return $this->getConstruction($article);
                break;
        }
    }

    private function uploadRemoteSai($currentPath)
    {
        if (function_exists('curl_file_create')) {
            $cFile = curl_file_create($currentPath);
        } else {
            $cFile = '@' . realpath($currentPath);
        }

        $dataPost = array('file_export'=> $cFile);
        $url = env("URL_SAI_EXPORT", "");
        $username = env("USERNAME_AUTH_SAI", "");
        $password = env("PASSWORD_AUTH_SAI", "");

        try {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $dataPost);
            curl_exec($ch);
            $response = curl_exec($ch);
            if ($response === false) {
                var_dump(curl_error($ch));
                Log::info( 'cURL Error: ' . curl_error($ch));
            }
            curl_close($ch);
        } catch (\Error $error) {
            var_dump($error);
        }
    }

    private function getMoveIn($article)
    {
        $moveIn = $article->move_in;
        switch ($moveIn) {
            case 1:
                return "即可能";
                break;
            case 2:
                return "要相談";
                break;
            case 3:
                return "{$article->move_in_year}年{$article->move_in_month}月";
                break;
            case 4:
                return "契約後{$article->move_in_contract_month}ヶ月";
                break;
        }

        return "-";
    }

    private function getGround($article)
    {
        $property = $article->property;
        $groups = [
            0 => "-",
            1 => "宅地",
            2 => "田",
            3 => "畑",
            4 => "山林",
            5 => "雑種地",
            6 => "原野",
            7 => "その他",
        ];

        switch ($property) {
            case "土地":
                return $groups[$article->ground] ?? "";
                break;
            case "一戸建て":
            case "マンション":
                return "宅地";
                break;
        }
    }

    private function roundFl($number)
    {
        return floor($number * 1000) / 1000;
    }

    private function getLine($article, $index = 1)
    {
        $direction = $this->getLandDirection($article->{"land_direction" . $index});
        $kind = $this->getLandKind($article->{"land_kind" . $index});
        $width = $article->{"land_kind" . $index};
        $frontage = $article->{"frontage" . $index};
        if (! empty($direction)) {
            $lineNumber = "";
            if (!empty($width)) {
                $lineNumber .= "幅員{$width}ｍ";
            }
            if (!empty($frontage)) {
                $lineNumber .= "間口{$frontage}ｍ";
            }
            return "{$direction}側{$kind} $lineNumber";
        }
        return "";
    }

    private function getLandDirection($direction)
    {
        $directions = [
            1 => "北",
            2 => "北東",
            3 => "東",
            4 => "南東",
            5 => "南",
            6 => "南西",
            7 => "西",
            8 => "北西"
        ];

        return $directions[$direction] ?? "";
    }

    private function getLandKind($kind)
    {
        $kinds = [
            1 => "公道",
            2 => "私道"
        ];

        return $kinds[$kind] ?? "";
    }

    private function getUseArea($value)
    {
        $area = [
            1 => "第一種低層住居専用地域",
            10 => "第二種低層住居専用地域",
            11 => "第一種中高層住居専用地域",
            2 => "第二種中高層住居専用地域",
            12 => "第一種住居地域",
            3 => "第二種住居地域",
            13 => "準住居地域",
            4 => "近隣商業地域",
            5 => "商業地域",
            6 => "準工業地域",
            7 => "工業地域",
            8 => "工業専用地域",
            14 => "田園住居地域",
            99 => "無",
        ];
        return $area[$value] ?? "";
    }

    private function getBalconyDirection($article)
    {
        $direction = [
            1 => "北",
            2 => "北東",
            3 => "東",
            4 => "南東",
            5 => "南",
            6 => "南西",
            7 => "西",
            8 => "北西"
        ];

        return $direction[$article->balcony_direction] ?? "";
    }

    private function getCurrentStatus($article)
    {

        $status = [
            4 => "居住中",
            5 => "空家",
            6 => "賃貸中",
            7 => "建築中",
            8 => "完成済",
            9 => "未着工",
            10 => "住居中",
            11 => "空室",
            12 => "賃貸中",
            13 => "不明"
        ];

        return $status[$article->current_status] ?? "-";
    }

    private function getStation($article, $index = 1, $bus = false, $time = true)
    {
        $station = "";

        switch ($index) {
            case 1:
                $station = "{$article->main_traffic_line}{$article->main_traffic_station}";
                if (empty($station)) {
                    break;
                }

                if ($time) {
                    if ($bus) {
                        $station .= "までバス{$article->main_traffic_bus_time}分";
                        if (empty($article->main_traffic_bus_time)) return "";
                    } else {
                        $station .= "まで徒歩{$article->main_traffic_time}分";
                        if (empty($article->main_traffic_time)) return "";
                    }
                }
                break;
            case 2:
                $station = "{$article->sub_traffic1_line}{$article->sub_traffic1_station}";
                if (empty($station)) {
                    break;
                }

                if ($time) {
                    if ($bus) {
                        $station .= "までバス{$article->sub_traffic1_bus_time}分";
                        if (empty($article->sub_traffic1_bus_time)) return "";
                    } else {
                        $station .= "まで徒歩{$article->sub_traffic1_time}分";
                        if (empty($article->sub_traffic1_time)) return "";
                    }
                }
                break;
            case 3:
                $station = "{$article->sub_traffic2_line}{$article->sub_traffic2_station}";
                if (empty($station)) {
                    break;
                }

                if ($time) {
                    if ($bus) {
                        $station .= "までバス{$article->sub_traffic2_bus_time}分";
                        if (empty($article->sub_traffic2_bus_time)) return "";
                    } else {
                        $station .= "まで徒歩{$article->sub_traffic2_time}分";
                        if (empty($article->sub_traffic2_time)) return "";
                    }
                }
                break;
        }

        return $station;
    }

    private function getTotalArea($article)
    {
        $property = $article->property;
        switch ($property) {
            case "土地":
            case "一戸建て":
                return "";
                break;
        }

        $area = [
            1 => "壁芯",
            2 => "内法",
        ];

        return $area[$article->total_area] ?? "";
    }

    private function getManagementForm($article)
    {
        $property = $article->property;
        switch ($property) {
            case "土地":
            case "一戸建て":
                return "";
                break;
            case "マンション":
                break;
        }

        $managements = [
            0 => "-",
            1 => "委託",
            2 => "委託",
            3 => "委託",
            4 => "委託",
            5 => "自主管理"
        ];

        return $managements[$article->management_form] ?? "-";
    }

    private function getPropertyName(&$article)
    {
        if (empty($article->property)) {
            return "マンション";
        }
        if ($article->property == 1) {
            $article->management_company = "";
            return "土地";
        }
        if($article->property == 5){
            return  "一戸建て";
        }

        $article->management_company = "";
        return "一戸建て";
    }

    private function getHistory($article)
    {
        $prices = $article->prices()->orderBy("id")->get();
        $history = "";
        foreach ($prices as $key => $price) {
            if ($key == (count($prices) - 1)) {
                $history .= "{$price->price}万円";
            } else {
                $history .= "{$price->price}万円→";
            }
        }
        return $history;
    }

    private function getGas($article)
    {
        $gas = [
            0 => "",
            1 => "都市",
            2 => "プロパン",
            3 => "プロパン",
            4 => "オール電化"
        ];

        return $gas[$article->gas] ?? "";
    }

    private function getSewerage($article)
    {
        $property = $article->property;
        switch ($property) {
            case "マンション":
                return "";
                break;
        }

        $sewerage = [
            0 => "",
            1 => "下水",
            2 => "浄化槽",
            3 => "浄化槽",
        ];

        return $sewerage[$article->sewerage] ?? "";
    }

    private function getWaterSupply($article)
    {
        $property = $article->property;
        switch ($property) {
            case "マンション":
                return "";
                break;
        }

        $waters = [
            0 => "",
            1 => "公営",
            2 => "私設",
            3 => "井戸"
        ];

        return $waters[$article->water_supply] ?? "";
    }

    private function getCompanyManager($article)
    {
        $managers = [
              1 => "売主",
              2 => "売主",
              3 => "媒介",
              4 => "媒介",
              5 => "媒介",
              6 => "代理販売",
              7 => "媒介",
              8 => "代理販売",
              9 => "媒介",
        ];

        return $managers[$article->company_manner] ?? "";
    }

    private function getPropertySub($article)
    {
        $property = $article->property;
        if ($property == "土地") return "";
        if ($article->property_sub == 1) return "新築";
        return "中古";
    }

    private function getCustomField($article, $code)
    {
        $field = $article->customs()->where("code", $code)->first();
        return optional($field)->contents;
    }

    private function getParking($article)
    {
        $property = $article->property;
        $parking = $article->parking;
        $parkingOut = $article->parking_out;
        switch ($property) {
            case "土地":
                return "無";
                break;
            case "一戸建て":
                if ($parking == 0) return "-";
                if ($parking == 1) return "無";
                return "有";
                break;
            case "マンション":
                if ($parking == 1 && $parkingOut == 0) return "無";
                return "有";
                break;
        }

        return "";
    }

    private function getFloor($article)
    {
        $article->floor_plan_type = $article->floor_plan_type ?? 1;
        $property = $article->property;

        if ($property == "土地") {
            return "";
        }

        $type = [
            1 => "DK",
            2 => "LDK",
            3 => "R",
            4 => "K",
            5 => "SK",
            6 => "SDK",
            7 => "LK",
            8 => "SLK",
            9 => "SLDK"
        ];

        return $article->floor_plan . $type[$article->floor_plan_type];
    }

    private function getLandRight($article)
    {
        $lands = [
            0 => "",
            1 => "所有権",
            2 => "借地権のみ",
            3 => "所有権・借地権混在",
        ];

        return $lands[$article->land_right] ?? "";
    }

    private function getRoadBurden($article)
    {
        $roads = [
            0 => "無",
            1 => "有",
            2 => "有り",
            3 => ""
        ];

        return $roads[$article->road_burden] ?? "";
    }

    private function getKokudoho($article)
    {
        $kokudoho = [
            0 => "",
            1 => "届出要",
            2 => "届出不要"
        ];

        return $kokudoho[$article->kokudoho] ?? "";
    }

    private function getTerrain($article)
    {
        $terrain = [
            0 => "",
            1 => "平坦",
            2 => "高台",
            3 => "低地",
            4 => "ひな壇",
            5 => "傾斜地",
            9 => "その他",
        ];

        return $terrain[$article->terrain] ?? "";
    }

    private function getCityPlan($article)
    {
        $plans = [
            1 => "市街化区域",
            2 => "調整区域",
            3 => "非線引区域",
            4 => "区域外",
            5 => "準都市区域",
        ];

        return $plans[$article->city_plan] ?? "";
    }

    private function getPrimarySchool($article)
    {
        $school = $article->primarySchool;
        return optional($school)->name;
    }

    private function getSecondarySchool($article)
    {
        $school = $article->secondarySchool;
        return optional($school)->name;
    }

    private function getAddress2($article)
    {
        $address = $article->city;
        return optional($address)->name;
    }

    private function getAddress3($article)
    {
        $address = $article->town;
        return optional($address)->name;
    }
}
