<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use App\Models\Page;
use App\Models\Course;
use Illuminate\Support\Str;

class LaunchSite extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'brentwood:launch';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Clean up the sites redirects and launch';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $ids = [
            1, // home
            55, // admissions
                56, // apply
                57, // financial
                58, // info sessions
                101, // new families
                185, // contact us
            2, // why brentwood
                4, // choice
                12, // support
                3, // timetable
                7, // campus
                8, // philosophy
                    191, // mission vision values
                9, // legacy
            10, // student life
                5, // boarding
                14, // activities
                    11, // skiing & snowboarding
                15, // leadership
                13, // student services
            16, // academics
                17, // post secondary
                18, // ap
                137, // courses
                144, // faculty
                153, // facilities
                23, // grade 8
            24, // athletics
                145, // faciltiies
                146, // coaches
                19, // accomplishments
                37, // S & C
                25, // basketball
                26, // climbing
                27, // cross country
                28, // cross training
                151, // dance
                29, // field hockey
                152, // flag football,
                30, // golf
                31, // ice hockey
                154, // jui-jistu
                172, // multisport
                32, // odp
                33, // rowing
                34, // rugby
                38, // scuba
                35, // soccer
                36, // squash
                171, // swimming
                39, // tennis
                40, // volleyball
                41, // yoga
            42, // arts
                148, // facilities
                147, // faculty
                20, // arts gallery
                43, // sculpture
                44, // acting
                45, // dance
                46, // debate
                47, // drawning and painting
                159, // film & broadcast
                48, // media arts
                49, // music
                50, // musical
                51, // photography
                52, // pottery
                53, // robotics
                158, // theatre pro
                54, // woodwork
            74, // giving
            59, // community
                186, // current families
                187, // alumni
                96, // staff
                190, // students
                76, // news
            60, // login
            61, // live
            66, // inquiry
                67, // inquiry content
            73, // alumni
                183, // oba
                175, // archives
                169, // reunion
            173, // contact us
            69, // news
            188, // accommodation
            70, // current families
            71, // students
            72, // staff
        ];

        $course_ids = [
            // arts
            82,
            83,
            84,
            85,
            86,
            87,
            88,
            90,
            91,
            92,
            93,
            94,
            95,
            96,
            97,
            98,
            99,
            100,
            101,
            102,
            103,
            104,
            105,
            106,
            107,
            108,
            109,
            110,
            111,
            112,
        ];

        $launch = $this->confirm('Are you sure you want to launch the site?', false);

        if ($launch) {
            $this->call('down');

            $pages = Page::whereIn('id', $ids)->get();
            $courses = Course::whereIn('id', $course_ids)->get();

            $this->info('PUBLISHING PAGES');

            $progress_bar = $this->output->createProgressBar(count($pages));
            $progress_bar->start();

            $pages->each(function ($page) use ($progress_bar) {
                $version = $page->getDraftVersion();
                if ($version->redirect) {
                    if (Str::contains($version->redirect, 'brentwood')) {
                        $version->redirect = null;
                        $version->save();
                    }
                }
                $page->publish();
                $progress_bar->advance();
            });

            $progress_bar->finish();

            $this->newLine();
            $this->newLine();

            $this->info('PUBLISHING COURSES');

            $progress_bar = $this->output->createProgressBar(count($courses));
            $progress_bar->start();

            $courses->each(function ($course) use ($progress_bar) {
                $course->publish();
                $progress_bar->advance();
            });

            $progress_bar->finish();

            $this->newLine();

            $this->call('cache:clear');

            $this->info('ADDING REDIRECTS');
            $this->call('db:seed', [ '--class' => 'PageRedirectsSeeder', '--force' => true]);

            $this->newLine();
            $this->info('REFRESHING PHOTOS');
            $this->call('brentwood:refresh-photos');

            $this->newLine();
            $this->info('REFRESHING VIDEOS');
            $this->call('brentwood:refresh-videos');

            /*
            if (!$this->option('skip-blogs')) {
                $this->newLine();
                $this->call('db:seed', [ '--class' => 'BlogsSeeder', '--force' => true]);
            }
             */

            $this->newLine();

            $this->call('up');

            $this->newLine();
            $this->info('PUBLISHING COMPLETE');
        }
    }
}
