<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\DTO\SearchData;

class PostFileDownLog extends Model
{
    protected $table = 'tb_post_file_down_log';
    protected $primaryKey = 'id';

    public $keyType = 'int';
    public $incrementing = true;
    public $timestamps = true;

    const CREATED_AT = 'created_at';
    const UPDATED_AT = null;

    protected $guarded = [];

    public function board()
    {
        return $this->belongsTo(Board::class)->withDefault();
    }

    public function post()
    {
        return $this->belongsTo(Post::class)->withDefault();
    }

    public function postFile()
    {
        return $this->belongsTo(PostFile::class)->withDefault();
    }

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

    /**
     * 게시글 첨부 파일 다운로드 기록 조회
     */
    public function data(SearchData $params): object
    {
        $query = $this->query();
        $query->select(
            'tb_board.name AS boardName',
            'tb_board.code',
            'tb_post_file.download',
            'tb_post_file_down_log.*',
            'tb_post_file.origin_name',
            'tb_post_file.file_size',
            'tb_post_file.is_image',
            'tb_post.subject',
            'users.name',
            'users.sid'
        );

        if ($params->keyword) {
            switch ($params->field) {
                case 'tb_post_file_down_log.id' :
                case 'tb_post_file_down_log.post_id' :
                case 'users.id' :
                case 'users.sid' :
                    $query->where($params->field, '=', $params->keyword);
                    break;
                case 'tb_post_file.origin_name' :
                case 'tb_post_file_down_log.ip_address' :
                case 'users.name' :
                case 'users.email' :
                    $query->where($params->field, 'LIKE', "%{$params->keyword}%");
                    break;
            }
        }

        if ($params->startDate || $params->endDate) {
            if ($params->startDate) {
                $query->where('tb_post_file_down_log.created_at', '>=', $params->startDate . ' 00:00:00');
            }
            if ($params->endDate) {
                $query->where('tb_post_file_down_log.created_at', '<=', $params->endDate . ' 23:59:59');
            }
        }

        if ($params->boardID) {
            $query->where('tb_post_file_down_log.board_id', '=', $params->boardID);
        }

        $query->join('tb_board', 'tb_board.id', '=', 'tb_post_file_down_log.board_id');
        $query->join('tb_post', 'tb_post.id', '=', 'tb_post_file_down_log.post_id');
        $query->leftjoin('tb_post_file', 'tb_post_file.id', '=', 'tb_post_file_down_log.post_file_id');
        $query->leftJoin('users', 'users.id', '=', 'tb_post_file.user_id');
        $query->orderByDesc('tb_post_file_down_log.id');

        $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
        $total = $this->count();
        $rows = $list->count();

        return (object)[
            'total' => $total,
            'rows' => $rows,
            'list' => $list
        ];
    }

    /**
     * 게시글 파일 다운로드 기록
     */
    public function register(array $params): int
    {
        return $this->insertGetId($params);
    }
}
