<?php

namespace App\Services;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class TextbookApiService
{
    protected string $baseUrl;

    public function __construct()
    {
        // QuestionBankService API 地址
        $baseUrl = config('services.question_bank.base_url', 'http://localhost:5015/api');
        // 移除末尾的 /，保持 baseUrl 以 /api 结尾
        $this->baseUrl = rtrim($baseUrl, '/');
    }

    /**
     * 获取教材系列列表
     */
    public function getTextbookSeries(array $params = []): array
    {
        try {
            $response = Http::timeout(30)->get($this->baseUrl . '/textbooks/series', $params);

            if ($response->successful()) {
                return $response->json();
            }

            Log::error('Failed to fetch textbook series', [
                'status' => $response->status(),
                'body' => $response->body()
            ]);

            return ['data' => [], 'meta' => []];
        } catch (\Exception $e) {
            Log::error('Error fetching textbook series', ['error' => $e->getMessage()]);
            return ['data' => [], 'meta' => []];
        }
    }

    /**
     * 创建教材系列
     */
    public function createTextbookSeries(array $data): array
    {
        try {
            $response = Http::timeout(30)->post($this->baseUrl . '/textbooks/series', $data);

            if ($response->successful()) {
                return $response->json();
            }

            Log::error('Failed to create textbook series', [
                'status' => $response->status(),
                'body' => $response->body()
            ]);

            throw new \Exception('Failed to create textbook series');
        } catch (\Exception $e) {
            Log::error('Error creating textbook series', ['error' => $e->getMessage()]);
            throw $e;
        }
    }

    /**
     * 更新教材系列
     */
    public function updateTextbookSeries(int $seriesId, array $data): array
    {
        try {
            $response = Http::timeout(30)->put($this->baseUrl . "/textbooks/series/{$seriesId}", $data);

            if ($response->successful()) {
                return $response->json();
            }

            Log::error('Failed to update textbook series', [
                'status' => $response->status(),
                'body' => $response->body()
            ]);

            throw new \Exception('Failed to update textbook series');
        } catch (\Exception $e) {
            Log::error('Error updating textbook series', ['error' => $e->getMessage()]);
            throw $e;
        }
    }

    /**
     * 删除教材系列
     */
    public function deleteTextbookSeries(int $seriesId): bool
    {
        try {
            $response = Http::timeout(30)->delete($this->baseUrl . "/textbooks/series/{$seriesId}");

            if ($response->successful()) {
                return true;
            }

            Log::error('Failed to delete textbook series', [
                'status' => $response->status(),
                'body' => $response->body()
            ]);

            return false;
        } catch (\Exception $e) {
            Log::error('Error deleting textbook series', ['error' => $e->getMessage()]);
            return false;
        }
    }

    /**
     * 删除教材
     */
    public function deleteTextbook(int $textbookId): bool
    {
        try {
            $response = Http::timeout(30)->delete($this->baseUrl . "/textbooks/{$textbookId}");

            if ($response->successful()) {
                return true;
            }

            Log::error('Failed to delete textbook', [
                'textbook_id' => $textbookId,
                'status' => $response->status(),
                'body' => $response->body()
            ]);

            return false;
        } catch (\Exception $e) {
            Log::error('Error deleting textbook', ['error' => $e->getMessage(), 'textbook_id' => $textbookId]);
            return false;
        }
    }

    /**
     * 获取教材列表
     */
    public function getTextbooks(array $params = []): array
    {
        try {
            $response = Http::timeout(30)->get($this->baseUrl . '/textbooks', $params);

            if ($response->successful()) {
                return $response->json();
            }

            Log::error('Failed to fetch textbooks', [
                'status' => $response->status(),
                'body' => $response->body()
            ]);

            return ['data' => [], 'meta' => []];
        } catch (\Exception $e) {
            Log::error('Error fetching textbooks', ['error' => $e->getMessage()]);
            return ['data' => [], 'meta' => []];
        }
    }

    /**
     * 获取单个教材
     */
    public function getTextbook(int $textbookId): ?array
    {
        try {
            $response = Http::timeout(30)->get($this->baseUrl . "/textbooks/{$textbookId}");

            if ($response->successful()) {
                return $response->json('data');
            }

            Log::error('Failed to fetch textbook', [
                'status' => $response->status(),
                'body' => $response->body()
            ]);

            return null;
        } catch (\Exception $e) {
            Log::error('Error fetching textbook', ['error' => $e->getMessage()]);
            return null;
        }
    }

    /**
     * 创建教材
     */
    public function createTextbook(array $data): array
    {
        try {
            $response = Http::timeout(30)->post($this->baseUrl . '/textbooks', $data);

            if ($response->successful()) {
                return $response->json();
            }

            Log::error('Failed to create textbook', [
                'status' => $response->status(),
                'body' => $response->body()
            ]);

            throw new \Exception('Failed to create textbook');
        } catch (\Exception $e) {
            Log::error('Error creating textbook', ['error' => $e->getMessage()]);
            throw $e;
        }
    }

    /**
     * 更新教材
     */
    public function updateTextbook(int $textbookId, array $data): array
    {
        try {
            $response = Http::timeout(30)->put($this->baseUrl . "/textbooks/{$textbookId}", $data);

            if ($response->successful()) {
                return $response->json();
            }

            Log::error('Failed to update textbook', [
                'status' => $response->status(),
                'body' => $response->body()
            ]);

            throw new \Exception('Failed to update textbook');
        } catch (\Exception $e) {
            Log::error('Error updating textbook', ['error' => $e->getMessage()]);
            throw $e;
        }
    }

    /**
     * 删除教材目录节点
     */
    public function deleteTextbookCatalog(int $catalogId): bool
    {
        try {
            $response = Http::timeout(30)->delete($this->baseUrl . "/textbooks/catalog/{$catalogId}");

            if ($response->successful()) {
                return true;
            }

            Log::error('Failed to delete textbook catalog', [
                'catalog_id' => $catalogId,
                'status' => $response->status(),
                'body' => $response->body()
            ]);

            return false;
        } catch (\Exception $e) {
            Log::error('Error deleting textbook catalog', ['error' => $e->getMessage(), 'catalog_id' => $catalogId]);
            return false;
        }
    }

    /**
     * 获取教材目录树
     */
    public function getTextbookCatalog(int $textbookId, string $format = 'tree'): array
    {
        try {
            $response = Http::timeout(30)->get($this->baseUrl . "/textbooks/{$textbookId}/catalog", [
                'format' => $format
            ]);

            if ($response->successful()) {
                return $response->json('data');
            }

            Log::error('Failed to fetch textbook catalog', [
                'status' => $response->status(),
                'body' => $response->body()
            ]);

            return [];
        } catch (\Exception $e) {
            Log::error('Error fetching textbook catalog', ['error' => $e->getMessage()]);
            return [];
        }
    }

    /**
     * 预览教材命名
     */
    public function previewTextbookNaming(array $textbookData, array $seriesData): array
    {
        try {
            $response = Http::timeout(30)->post($this->baseUrl . '/textbooks/naming-preview', [
                'textbook' => $textbookData,
                'series' => $seriesData
            ]);

            if ($response->successful()) {
                return $response->json('data');
            }

            Log::error('Failed to preview textbook naming', [
                'status' => $response->status(),
                'body' => $response->body()
            ]);

            return [];
        } catch (\Exception $e) {
            Log::error('Error previewing textbook naming', ['error' => $e->getMessage()]);
            return [];
        }
    }

    /**
     * 导入教材元信息
     */
    public function importTextbookMetadata($file, string $commitMode = 'overwrite'): array
    {
        try {
            $response = Http::timeout(300)
                ->attach('file', file_get_contents($file->getPathname()), $file->getClientOriginalName())
                ->post($this->baseUrl . '/textbooks/import/meta', [
                    'commit_mode' => $commitMode
                ]);

            if ($response->successful()) {
                return $response->json();
            }

            Log::error('Failed to import textbook metadata', [
                'status' => $response->status(),
                'body' => $response->body()
            ]);

            throw new \Exception('Failed to import textbook metadata');
        } catch (\Exception $e) {
            Log::error('Error importing textbook metadata', ['error' => $e->getMessage()]);
            throw $e;
        }
    }

    /**
     * 导入教材目录
     */
    public function importTextbookCatalog(int $textbookId, $file, string $commitMode = 'overwrite'): array
    {
        try {
            $response = Http::timeout(300)
                ->attach('file', file_get_contents($file->getPathname()), $file->getClientOriginalName())
                ->post($this->baseUrl . '/textbooks/import/catalog', [
                    'textbook_id' => $textbookId,
                    'commit_mode' => $commitMode
                ]);

            if ($response->successful()) {
                return $response->json();
            }

            Log::error('Failed to import textbook catalog', [
                'status' => $response->status(),
                'body' => $response->body()
            ]);

            throw new \Exception('Failed to import textbook catalog');
        } catch (\Exception $e) {
            Log::error('Error importing textbook catalog', ['error' => $e->getMessage()]);
            throw $e;
        }
    }

    /**
     * 提交导入任务
     */
    public function commitImportJob(int $jobId): array
    {
        try {
            $response = Http::timeout(300)->post($this->baseUrl . "/api/textbooks/import/{$jobId}/commit");

            if ($response->successful()) {
                return $response->json();
            }

            Log::error('Failed to commit import job', [
                'status' => $response->status(),
                'body' => $response->body()
            ]);

            throw new \Exception('Failed to commit import job');
        } catch (\Exception $e) {
            Log::error('Error committing import job', ['error' => $e->getMessage()]);
            throw $e;
        }
    }

    /**
     * 获取导入任务列表
     */
    public function getImportJobs(array $params = []): array
    {
        try {
            $response = Http::timeout(30)->get($this->baseUrl . '/textbooks/import/jobs', $params);

            if ($response->successful()) {
                return $response->json();
            }

            Log::error('Failed to fetch import jobs', [
                'status' => $response->status(),
                'body' => $response->body()
            ]);

            return ['data' => [], 'meta' => []];
        } catch (\Exception $e) {
            Log::error('Error fetching import jobs', ['error' => $e->getMessage()]);
            return ['data' => [], 'meta' => []];
        }
    }

    /**
     * 获取单个导入任务
     */
    public function getImportJob(int $jobId): ?array
    {
        try {
            $response = Http::timeout(30)->get($this->baseUrl . "/api/textbooks/import/jobs/{$jobId}");

            if ($response->successful()) {
                return $response->json('data');
            }

            Log::error('Failed to fetch import job', [
                'status' => $response->status(),
                'body' => $response->body()
            ]);

            return null;
        } catch (\Exception $e) {
            Log::error('Error fetching import job', ['error' => $e->getMessage()]);
            return null;
        }
    }
}
