<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use app\models\ColourScheme;

class ColourSchemeController extends Controller
{
    public $layout = 'main';

    public function behaviors(): array
    {
        return [
            'access' => [
                'class' => AccessControl::class,
                'rules' => [[
                    'allow' => true,
                    'roles' => ['@'],
                ]],
            ],
            'verbs' => [
                'class'   => VerbFilter::class,
                'actions' => ['delete' => ['post']],
            ],
        ];
    }

    // ================================================================
    // INDEX
    // ================================================================
    public function actionIndex(): string
    {
        $filter = Yii::$app->request->get('type', '');
        $query  = ColourScheme::find()->orderBy(['type' => SORT_ASC, 'item' => SORT_ASC]);

        if (array_key_exists($filter, ColourScheme::TYPE_LABELS)) {
            $query->where(['type' => $filter]);
        }

        $schemes = $query->all();

        return $this->render('index', [
            'schemes' => $schemes,
            'filter'  => $filter,
        ]);
    }

    // ================================================================
    // CREATE
    // ================================================================
    public function actionCreate()
    {
        $model = new ColourScheme();
        $model->colour = '#000000';

        if ($model->load(Yii::$app->request->post())) {
            $userId = Yii::$app->user->id;
            $model->created_by = $userId;
            $model->updated_by = $userId;
            $model->created_at = date('Y-m-d H:i:s');
            $model->updated_at = date('Y-m-d H:i:s');

            if ($model->save()) {
                Yii::$app->session->setFlash('success', "Colour scheme '{$model->item}' created.");
                return $this->redirect(['colour-scheme/index']);
            }
        }

        return $this->render('create', ['model' => $model]);
    }

    // ================================================================
    // UPDATE
    // ================================================================
    public function actionUpdate(int $id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) {
            $model->updated_by = Yii::$app->user->id;
            $model->updated_at = date('Y-m-d H:i:s');

            if ($model->save()) {
                Yii::$app->session->setFlash('success', "Colour scheme '{$model->item}' updated.");
                return $this->redirect(['colour-scheme/index']);
            }
        }

        return $this->render('update', ['model' => $model]);
    }

    // ================================================================
    // DELETE
    // ================================================================
    public function actionDelete(int $id)
    {
        $model = $this->findModel($id);
        $item  = $model->item;
        $model->delete();
        Yii::$app->session->setFlash('success', "Colour scheme '{$item}' deleted.");
        return $this->redirect(['colour-scheme/index']);
    }

    // ================================================================
    // Helper
    // ================================================================
    protected function findModel(int $id): ColourScheme
    {
        $model = ColourScheme::findOne($id);
        if ($model === null) {
            throw new NotFoundHttpException('Colour scheme not found.');
        }
        return $model;
    }
}
