<?php

declare(strict_types=1);

namespace App\Models;

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

/**
 * User Statistics – v2 schema.
 *
 * Single row per user (user_id is PK).
 * All counters are unsignedBigInteger with default 0.
 *
 * All updates MUST go through App\Services\UserStatsService.
 */
class UserStatistic extends Model
{
    protected $table = 'user_statistics';
    protected $primaryKey = 'user_id';
    public $incrementing = false;
    protected $keyType = 'int';

    protected $fillable = [
        'user_id',

        // Creator upload activity
        'uploads_count',

        // Creator-received metrics
        'downloads_received_count',
        'artwork_views_received_count',
        'awards_received_count',
        'favorites_received_count',
        'comments_received_count',
        'reactions_received_count',

        // Social stats (managed by FollowService)
        'followers_count',
        'following_count',

        // Profile / discovery
        'profile_views_count',

        // Activity timestamps
        'last_upload_at',
        'last_active_at',
    ];

    protected $casts = [
        'user_id'                      => 'integer',
        'uploads_count'                => 'integer',
        'downloads_received_count'     => 'integer',
        'artwork_views_received_count' => 'integer',
        'awards_received_count'        => 'integer',
        'favorites_received_count'     => 'integer',
        'comments_received_count'      => 'integer',
        'reactions_received_count'     => 'integer',
        'followers_count'              => 'integer',
        'following_count'              => 'integer',
        'profile_views_count'          => 'integer',
        'last_upload_at'               => 'datetime',
        'last_active_at'               => 'datetime',
    ];

    public $timestamps = true;

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }
}
