<?php

namespace app\models;

use yii\db\ActiveRecord;

/**
 * Model for lms_colour_scheme
 *
 * @property int    $id
 * @property int|null $tenant_id
 * @property string $type
 * @property string $item
 * @property string $field
 * @property string $colour
 * @property int    $created_by
 * @property int    $updated_by
 * @property string $created_at
 * @property string $updated_at
 *
 * @property User   $createdBy
 * @property User   $updatedBy
 */
class ColourScheme extends ActiveRecord
{
    const TYPE_LABELS = [
        'TE' => 'Theme',
        'TH' => 'Table Header',
        'BN' => 'Button',
        'IN' => 'Icon',
    ];

    public static function tableName(): string
    {
        return '{{%colour_scheme}}';
    }

    public function rules(): array
    {
        return [
            [['type', 'item', 'field', 'colour'], 'required'],
            [['type'],      'in', 'range' => array_keys(self::TYPE_LABELS)],
            [['item', 'field'], 'string', 'max' => 255],
            [['colour'],    'string', 'max' => 7],
            [['colour'],    'match', 'pattern' => '/^#[0-9A-Fa-f]{6}$/', 'message' => 'Colour must be a valid hex code (e.g. #3498db).'],
            [['tenant_id', 'created_by', 'updated_by'], 'integer'],
            [['tenant_id'], 'default', 'value' => null],
        ];
    }

    public function attributeLabels(): array
    {
        return [
            'id'         => 'ID',
            'tenant_id'  => 'Tenant',
            'type'       => 'Type',
            'item'       => 'Item',
            'field'      => 'Field / Description',
            'colour'     => 'Colour',
            'created_by' => 'Created By',
            'updated_by' => 'Updated By',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    public function getTypeLabel(): string
    {
        return self::TYPE_LABELS[$this->type] ?? $this->type;
    }

    public function getCreatedBy(): \yii\db\ActiveQuery
    {
        return $this->hasOne(User::class, ['id' => 'created_by']);
    }

    public function getUpdatedBy(): \yii\db\ActiveQuery
    {
        return $this->hasOne(User::class, ['id' => 'updated_by']);
    }
}
