<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace Applications\sample\modules\article\service\admin;

use app\service\admin\BaseAdminService;
use app\service\RequestHelper;
use Applications\sample\modules\article\core\CoreArticleService;
use Applications\sample\modules\article\repository\ArticleEventLogRepository;
use Applications\sample\modules\article\repository\ArticleRepository;
use think\Request;

/**
 * Article Service
 * @package Applications\sample\modules\article\service\admin
 */
class ArticleService extends BaseAdminService
{
    /**
     * 后台列表（含分类筛选 + 关联分类名）
     */
    public function list(Request $request): array
    {
        $page = $this->repository()->paginateForAdmin([
            'keyword' => $request->get('keyword', ''),
            'category_id' => $request->get('category_id', ''),
            'status' => $request->get('status', ''),
        ], RequestHelper::parseLimit($request, 'limit', 10, 100));

        return ['code' => 0, 'data' => $page];
    }

    /**
     * 详情（含分类名，用于编辑页回显）
     */
    public function detail(int $id): array
    {
        $article = $this->repository()->findById($id);
        if (!$article) {
            return ['code' => 1, 'msg' => '文章不存在'];
        }
        $data = $article->toArray();
        $category = \Applications\sample\modules\category\model\Category::find($article->category_id);
        $data['category_name'] = $category->name ?? '';
        $data['event_logs'] = $this->eventLogs()->listByArticleId($id);
        return ['code' => 0, 'data' => $data];
    }

    /**
     * 保存（新增字段：category_id, audio_url, source_url）
     */
    public function save(Request $request): array
    {
        $id = (int)$request->post('id', 0);
        $payload = [
            'id' => $id,
            'title' => $request->post('title', ''),
            'category_id' => $request->post('category_id', 0),
            'cover_image' => $request->post('cover_image', ''),
            'content' => $request->post('content', ''),
            'audio_url' => $request->post('audio_url', ''),
            'source_url' => $request->post('source_url', ''),
            'price' => $request->post('price', 0),
            'status' => $request->post('status', 1),
        ];

        return $id > 0 ? $this->core()->update($id, $payload) : $this->core()->create($payload);
    }


    /**
     * 删除文章
     * @param Request $request
     * @return array
     */
    public function delete(Request $request): array
    {
        return $this->core()->delete((int)$request->post('id', 0));
    }

    /**
     * 获取核心文章服务实例
     * @return CoreArticleService
     */
    private function core(): CoreArticleService
    {
        return $this->app->make(CoreArticleService::class);
    }

    /**
     * 获取文章仓库实例
     * @return ArticleRepository
     */
    private function repository(): ArticleRepository
    {
        return $this->app->make(ArticleRepository::class);
    }

    /**
     * 获取文章事件日志仓库实例
     * @return ArticleEventLogRepository
     */
    private function eventLogs(): ArticleEventLogRepository
    {
        return $this->app->make(ArticleEventLogRepository::class);
    }
}
