<?php

namespace App\Models;

use App\Models\Tenants\Tenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class FcmSendLog extends Model
{
    /**
     * 상태 상수
     */
    public const STATUS_PENDING = 'pending';

    public const STATUS_SENDING = 'sending';

    public const STATUS_COMPLETED = 'completed';

    public const STATUS_FAILED = 'failed';

    protected $fillable = [
        'tenant_id',
        'user_id',
        'sender_id',
        'title',
        'body',
        'channel_id',
        'type',
        'platform',
        'data',
        'total_count',
        'success_count',
        'failure_count',
        'invalid_token_count',
        'success_rate',
        'status',
        'error_message',
        'completed_at',
    ];

    protected $casts = [
        'data' => 'array',
        'total_count' => 'integer',
        'success_count' => 'integer',
        'failure_count' => 'integer',
        'invalid_token_count' => 'integer',
        'success_rate' => 'decimal:2',
        'completed_at' => 'datetime',
    ];

    /**
     * 발송자 (MNG 관리자)
     */
    public function sender(): BelongsTo
    {
        return $this->belongsTo(User::class, 'sender_id');
    }

    /**
     * 테넌트
     */
    public function tenant(): BelongsTo
    {
        return $this->belongsTo(Tenant::class);
    }

    /**
     * 대상 사용자
     */
    public function targetUser(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    /**
     * Scope: 특정 발송자의 로그
     */
    public function scopeBySender($query, int $senderId)
    {
        return $query->where('sender_id', $senderId);
    }

    /**
     * Scope: 특정 상태
     */
    public function scopeByStatus($query, string $status)
    {
        return $query->where('status', $status);
    }

    /**
     * Scope: 최근 N일
     */
    public function scopeRecent($query, int $days = 7)
    {
        return $query->where('created_at', '>=', now()->subDays($days));
    }

    /**
     * 발송 시작 표시
     */
    public function markAsSending(): void
    {
        $this->update(['status' => self::STATUS_SENDING]);
    }

    /**
     * 발송 완료 표시
     */
    public function markAsCompleted(array $summary): void
    {
        $this->update([
            'status' => self::STATUS_COMPLETED,
            'total_count' => $summary['total'],
            'success_count' => $summary['success'],
            'failure_count' => $summary['failure'],
            'invalid_token_count' => $summary['invalid_tokens'],
            'success_rate' => $summary['success_rate'],
            'completed_at' => now(),
        ]);
    }

    /**
     * 발송 실패 표시
     */
    public function markAsFailed(string $errorMessage): void
    {
        $this->update([
            'status' => self::STATUS_FAILED,
            'error_message' => $errorMessage,
            'completed_at' => now(),
        ]);
    }
}
