<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class UserDetail extends Model
{
    protected $fillable = [
        'user_id',
        'type',
        'phone',
        'business_email',
        'company_short_name',
        'company_full_name',
        'company_description',
        'rating',
        'tags',
        'employee_count',
        'engineer_count',
        'n1_japanese_count',
        'established_year',
        'japanese_companies_performance',
        'company_logo',
        'strengths',
        'leader_name',
        'leader_position',
        'establishment_date',
        'charter_capital',
        'estimated_value',
        'headquarters_address',
        'branches',
        'article_link',
        'status',
        'hourly_rate_range',
        'key_technologies',
    ];

    protected $casts = [
        'tags'               => 'array',
        'strengths'          => 'array',
        'branches'           => 'array',
        'key_technologies'   => 'array',
        'establishment_date' => 'date',
        'rating'             => 'decimal:1',
        'charter_capital'    => 'decimal:2',
        'estimated_value'    => 'decimal:2',
    ];

    // Constants for user types
    const TYPE_REGULAR_USER = 0;

    const TYPE_COMPANY = 1;

    const TYPE_SOFTWARE_TEAM = 2;

    const TYPE_FREELANCER = 3;

    // Constants for status
    const STATUS_PENDING = 0;

    const STATUS_APPROVED = 1;

    const STATUS_REJECTED = 2;

    /**
     * Quan hệ với User (1-1)
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Kiểm tra loại tài khoản
     */
    public function isCompany(): bool
    {
        return $this->type === self::TYPE_COMPANY;
    }

    public function isSoftwareTeam(): bool
    {
        return $this->type === self::TYPE_SOFTWARE_TEAM;
    }

    public function isFreelancer(): bool
    {
        return $this->type === self::TYPE_FREELANCER;
    }

    public function isRegularUser(): bool
    {
        return $this->type === self::TYPE_REGULAR_USER;
    }

    /**
     * Kiểm tra trạng thái
     */
    public function isApproved(): bool
    {
        return $this->status === self::STATUS_APPROVED;
    }

    public function isPending(): bool
    {
        return $this->status === self::STATUS_PENDING;
    }

    public function isRejected(): bool
    {
        return $this->status === self::STATUS_REJECTED;
    }

    /**
     * Lấy tên hiển thị dựa trên loại tài khoản
     */
    public function getDisplayName(): string
    {
        return match ($this->type) {
            self::TYPE_COMPANY, self::TYPE_SOFTWARE_TEAM => $this->company_short_name ?? $this->company_full_name ?? $this->user->name,
            self::TYPE_FREELANCER => $this->user->name,
            default               => $this->user->name,
        };
    }

    /**
     * Lấy text mô tả loại tài khoản
     */
    public function getTypeText(): string
    {
        return match ($this->type) {
            self::TYPE_COMPANY       => 'Công ty',
            self::TYPE_SOFTWARE_TEAM => 'Nhóm phát triển phần mềm',
            self::TYPE_FREELANCER    => 'Freelancer',
            default                  => 'Người dùng thường',
        };
    }

    /**
     * Lấy text trạng thái
     */
    public function getStatusText(): string
    {
        return match ($this->status) {
            self::STATUS_APPROVED => 'Đã xác nhận',
            self::STATUS_REJECTED => 'Đã từ chối',
            default               => 'Chưa xác nhận',
        };
    }
}
