<?php

namespace App\Models\Post\User;

use App\Models\User;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Spatie\Image\Manipulations;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

class UserDoc extends Model implements HasMedia
{
    use HasFactory;
    use InteractsWithMedia;

    /**
     * Attributes that are guarded
     *
     * @var array
     */
    protected $guarded = [];

    /**
     * Table
     *
     * @var string
     */
    protected $table = 'user_docs';

    /**
     * The relations to eager load on every query.
     *
     * @var array
     */
    protected $with = [
        'media',
    ];

    /**
     * Media collections
     */
    public function registerMediaCollections(): void
    {
        $this->addMediaCollection('main');
    }

    /**
     * Media conversations
     */
    public function registerMediaConversions(?Media $media = null): void
    {
        $this->addMediaConversion('thumb150x150')
            ->fit(Manipulations::FIT_CONTAIN, 150, 150);
    }

    /**
     * User
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Entrepreneurship types
     *
     * ENTREPRENEUR
     */
    public const ENTREPRENEUR = 'tel';

    /**
     * PRIVATE ENTERPRISE
     */
    public const PRIVATE_ENTERPRISE = 'hk';

    /**
     * PARTNERSHIP ENTERPRISE
     */
    public const PARTNERSHIP_ENTERPRISE = 'hj';

    /**
     * Government
     */
    public const GOVERNMENT = 'gov';

    /**
     * Closed joint stock
     */
    public const CLOSED_JOINT_STOCK = 'closed_joint_stock';

    /**
     * Open joint stock
     */
    public const OPEN_JOINT_STOCK = 'open_joint_stock';

    /**
     * Corparation types
     */
    public static function corparationTypes(): array
    {
        return [
            self::ENTREPRENEUR => __('Entrepreneur'),
            self::PRIVATE_ENTERPRISE => __('Private enterprise'),
            self::PARTNERSHIP_ENTERPRISE => __('Partnership enterprise'),
            self::GOVERNMENT => __('Government company'),
            self::CLOSED_JOINT_STOCK => __('Closed joint stock company'),
            self::OPEN_JOINT_STOCK => __('Open joint-stock company'),
        ];
    }

    /**
     * Formatted corparation type
     */
    public function formattedCorparationType(): Attribute
    {
        return Attribute::make(
            get: fn () => self::corparationTypes()[$this->corporation_type] ?? ''
        );
    }

    /**
     * Formatted corparation name
     */
    public function formattedCorparationName(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->formattedCorparationType.' '.$this->corporation_name
        );
    }
}
