<?php

namespace App\Controllers;

use App\Models\ArticleModel;
use App\Models\IndustryModel;
use App\Models\SettingModel;
use CodeIgniter\Exceptions\PageNotFoundException;

class IndustryPages extends BaseController
{
    public function industries()
    {
        $settings = (new SettingModel())->values([
            "call_phone_display" => "+91 98765 43210",
            "call_phone_href" => "+919876543210",
            "whatsapp_number" => "919876543210",
            "social_instagram" => "",
            "social_behance" => "",
            "social_facebook" => "",
            "social_linkedin" => "",
            "social_youtube" => "",
            "social_pinterest" => "",
        ]);

        return view("industries_directory", [
            "industries" => (new IndustryModel())
                ->where("is_active", 1)
                ->orderBy("sort_order", "ASC")
                ->orderBy("name", "ASC")
                ->findAll(),
            "phoneDisplay" => $settings["call_phone_display"],
            "phoneHref" => $settings["call_phone_href"],
            "whatsappNumber" => $settings["whatsapp_number"],
            "socialLinks" => $this->socialLinks($settings),
        ]);
    }

    public function directory()
    {
        $settings = (new SettingModel())->values([
            "call_phone_display" => "+91 98765 43210",
            "call_phone_href" => "+919876543210",
            "social_instagram" => "",
            "social_behance" => "",
            "social_facebook" => "",
            "social_linkedin" => "",
            "social_youtube" => "",
            "social_pinterest" => "",
        ]);

        return view("sitemap_page", [
            "articles" => (new ArticleModel())
                ->where("is_published", 1)
                ->groupStart()
                ->where("published_at <=", date("Y-m-d H:i:s"))
                ->orWhere("published_at", null)
                ->groupEnd()
                ->orderBy("is_featured", "DESC")
                ->orderBy("published_at", "DESC")
                ->findAll(100),
            "industries" => (new IndustryModel())
                ->where("is_active", 1)
                ->orderBy("sort_order", "ASC")
                ->orderBy("name", "ASC")
                ->findAll(),
            "phoneDisplay" => $settings["call_phone_display"],
            "phoneHref" => $settings["call_phone_href"],
            "socialLinks" => $this->socialLinks($settings),
        ]);
    }

    public function sitemap()
    {
        $industries = (new IndustryModel())
            ->where("is_active", 1)
            ->orderBy("updated_at", "DESC")
            ->findAll();
        $articles = (new ArticleModel())
            ->where("is_published", 1)
            ->groupStart()
            ->where("published_at <=", date("Y-m-d H:i:s"))
            ->orWhere("published_at", null)
            ->groupEnd()
            ->orderBy("updated_at", "DESC")
            ->findAll(250);
        $urls = [
            [
                "location" => base_url(),
                "modified" => date("Y-m-d"),
                "priority" => "1.0",
            ],
            [
                "location" => base_url("logo-design-india"),
                "modified" => date("Y-m-d"),
                "priority" => "0.9",
            ],
            [
                "location" => base_url("logo-redesign-india"),
                "modified" => date("Y-m-d"),
                "priority" => "0.9",
            ],
            [
                "location" => base_url("logo-redesign-scorecard"),
                "modified" => date("Y-m-d"),
                "priority" => "0.9",
            ],
            [
                "location" => base_url("brand-guide"),
                "modified" => $articles
                    ? date("Y-m-d", strtotime($articles[0]["updated_at"]))
                    : date("Y-m-d"),
                "priority" => "0.9",
            ],
            [
                "location" => base_url("industries"),
                "modified" => $industries
                    ? date("Y-m-d", strtotime($industries[0]["updated_at"]))
                    : date("Y-m-d"),
                "priority" => "0.9",
            ],
        ];
        foreach ($articles as $article) {
            $urls[] = [
                "location" => base_url("brand-guide/" . $article["slug"]),
                "modified" => $article["updated_at"]
                    ? date("Y-m-d", strtotime($article["updated_at"]))
                    : date("Y-m-d"),
                "priority" => "0.7",
            ];
        }
        foreach ($industries as $industry) {
            $urls[] = [
                "location" => base_url("logo-design-for-" . $industry["slug"]),
                "modified" => $industry["updated_at"]
                    ? date("Y-m-d", strtotime($industry["updated_at"]))
                    : date("Y-m-d"),
                "priority" => "0.8",
            ];
        }

        return $this->response->setContentType("application/xml")->setBody(
            view("sitemap", [
                "urls" => $urls,
            ]),
        );
    }

    public function show(string $slug)
    {
        $industries = new IndustryModel();
        $industry = $industries
            ->where("slug", strtolower($slug))
            ->where("is_active", 1)
            ->first();
        if (!$industry) {
            throw PageNotFoundException::forPageNotFound();
        }

        $settings = (new SettingModel())->values([
            "call_phone_display" => "+91 98765 43210",
            "call_phone_href" => "+919876543210",
            "whatsapp_number" => "919876543210",
            "social_instagram" => "",
            "social_behance" => "",
            "social_facebook" => "",
            "social_linkedin" => "",
            "social_youtube" => "",
            "social_pinterest" => "",
        ]);

        return view("industry", [
            "industry" => $industry,
            "relatedIndustries" => $industries
                ->where("is_active", 1)
                ->where("id !=", $industry["id"])
                ->orderBy("sort_order", "ASC")
                ->findAll(4),
            "phoneDisplay" => $settings["call_phone_display"],
            "phoneHref" => $settings["call_phone_href"],
            "whatsappNumber" => $settings["whatsapp_number"],
            "socialLinks" => $this->socialLinks($settings),
        ]);
    }

    private function socialLinks(array $settings): array
    {
        $links = [];
        foreach (
            [
                "Instagram" => "social_instagram",
                "Behance" => "social_behance",
                "Facebook" => "social_facebook",
                "LinkedIn" => "social_linkedin",
                "YouTube" => "social_youtube",
                "Pinterest" => "social_pinterest",
            ]
            as $label => $key
        ) {
            if (!empty($settings[$key])) {
                $links[$label] = $settings[$key];
            }
        }
        return $links;
    }
}
