<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Routing\Controller;

class ApiDocumentationController extends Controller
{
    public function index()
    {
        return view('admin.api-documentation');
    }

    public function swagger()
    {
        $swagger = [
            'openapi' => '3.0.0',
            'info' => [
                'title' => 'Hooshi API Documentation',
                'description' => 'تمام وب سرویس‌های API پروژه هوشی',
                'version' => '1.0.0',
                'contact' => [
                    'email' => 'support@hooshi.com'
                ]
            ],
            'servers' => [
                [
                    'url' => env('APP_URL', 'http://localhost'),
                    'description' => 'سرور پروژه هوشی'
                ]
            ],
            'components' => [
                'securitySchemes' => [
                    'bearerAuth' => [
                        'type' => 'http',
                        'scheme' => 'bearer',
                        'bearerFormat' => 'JWT'
                    ]
                ],
                'schemas' => [
                    'User' => [
                        'type' => 'object',
                        'properties' => [
                            'id' => ['type' => 'integer'],
                            'email' => ['type' => 'string', 'format' => 'email'],
                            'phone' => ['type' => 'string'],
                            'full_name' => ['type' => 'string'],
                            'avatar' => ['type' => 'string', 'nullable' => true],
                            'status' => ['type' => 'string', 'enum' => ['active', 'inactive', 'banned', 'pending']],
                            'email_verified' => ['type' => 'boolean'],
                            'phone_verified' => ['type' => 'boolean'],
                            'created_at' => ['type' => 'string', 'format' => 'date-time']
                        ]
                    ],
                    'LoginRequest' => [
                        'type' => 'object',
                        'required' => ['email', 'password'],
                        'properties' => [
                            'email' => ['type' => 'string', 'format' => 'email', 'example' => 'user@example.com'],
                            'password' => ['type' => 'string', 'format' => 'password', 'example' => 'password123']
                        ]
                    ],
                    'OtpRequest' => [
                        'type' => 'object',
                        'required' => ['email_or_phone', 'type'],
                        'properties' => [
                            'email_or_phone' => ['type' => 'string', 'example' => 'user@example.com'],
                            'type' => ['type' => 'string', 'enum' => ['register', 'login', 'reset_password']]
                        ]
                    ],
                    'OtpVerifyRequest' => [
                        'type' => 'object',
                        'required' => ['email_or_phone', 'code', 'type'],
                        'properties' => [
                            'email_or_phone' => ['type' => 'string', 'example' => 'user@example.com'],
                            'code' => ['type' => 'string', 'example' => '123456'],
                            'type' => ['type' => 'string', 'enum' => ['register', 'login', 'reset_password']]
                        ]
                    ],
                    'RegisterRequest' => [
                        'type' => 'object',
                        'required' => ['email', 'password', 'full_name'],
                        'properties' => [
                            'email' => ['type' => 'string', 'format' => 'email'],
                            'password' => ['type' => 'string', 'format' => 'password'],
                            'full_name' => ['type' => 'string'],
                            'phone' => ['type' => 'string', 'nullable' => true]
                        ]
                    ],
                    'UpdateProfileRequest' => [
                        'type' => 'object',
                        'properties' => [
                            'full_name' => ['type' => 'string'],
                            'phone' => ['type' => 'string'],
                            'avatar' => ['type' => 'string', 'format' => 'binary']
                        ]
                    ],
                    'ErrorResponse' => [
                        'type' => 'object',
                        'properties' => [
                            'success' => ['type' => 'boolean', 'example' => false],
                            'message' => ['type' => 'string'],
                            'errors' => ['type' => 'object', 'nullable' => true]
                        ]
                    ],
                    'SuccessResponse' => [
                        'type' => 'object',
                        'properties' => [
                            'success' => ['type' => 'boolean', 'example' => true],
                            'message' => ['type' => 'string'],
                            'data' => ['type' => 'object']
                        ]
                    ]
                ]
            ],
            'paths' => [
                '/api/auth/login' => [
                    'post' => [
                        'tags' => ['Authentication'],
                        'summary' => 'ورود به سیستم',
                        'description' => 'کاربر می‌تواند با ایمیل و رمز عبور وارد سیستم شود',
                        'requestBody' => [
                            'required' => true,
                            'content' => [
                                'application/json' => [
                                    'schema' => ['$ref' => '#/components/schemas/LoginRequest']
                                ]
                            ]
                        ],
                        'responses' => [
                            '200' => [
                                'description' => 'ورود موفق',
                                'content' => [
                                    'application/json' => [
                                        'schema' => [
                                            'type' => 'object',
                                            'properties' => [
                                                'success' => ['type' => 'boolean', 'example' => true],
                                                'message' => ['type' => 'string', 'example' => 'خوش آمدید'],
                                                'data' => [
                                                    'type' => 'object',
                                                    'properties' => [
                                                        'access_token' => ['type' => 'string'],
                                                        'user' => ['$ref' => '#/components/schemas/User']
                                                    ]
                                                ]
                                            ]
                                        ]
                                    ]
                                ]
                            ],
                            '401' => [
                                'description' => 'ایمیل یا رمز عبور نادرست',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/ErrorResponse']
                                    ]
                                ]
                            ],
                            '422' => [
                                'description' => 'داده‌های نامعتبر',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/ErrorResponse']
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                '/api/auth/register' => [
                    'post' => [
                        'tags' => ['Authentication'],
                        'summary' => 'ثبت نام کاربر جدید',
                        'description' => 'کاربر جدید می‌تواند ثبت نام کند',
                        'requestBody' => [
                            'required' => true,
                            'content' => [
                                'application/json' => [
                                    'schema' => ['$ref' => '#/components/schemas/RegisterRequest']
                                ]
                            ]
                        ],
                        'responses' => [
                            '201' => [
                                'description' => 'ثبت نام موفق',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/SuccessResponse']
                                    ]
                                ]
                            ],
                            '422' => [
                                'description' => 'داده‌های نامعتبر',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/ErrorResponse']
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                '/api/auth/send-otp' => [
                    'post' => [
                        'tags' => ['Authentication'],
                        'summary' => 'ارسال کد OTP',
                        'description' => 'کد تأیید به ایمیل یا شماره موبایل ارسال می‌شود',
                        'requestBody' => [
                            'required' => true,
                            'content' => [
                                'application/json' => [
                                    'schema' => ['$ref' => '#/components/schemas/OtpRequest']
                                ]
                            ]
                        ],
                        'responses' => [
                            '200' => [
                                'description' => 'کد OTP ارسال شد',
                                'content' => [
                                    'application/json' => [
                                        'schema' => [
                                            'type' => 'object',
                                            'properties' => [
                                                'success' => ['type' => 'boolean', 'example' => true],
                                                'message' => ['type' => 'string', 'example' => 'کد تأیید ارسال شده است']
                                            ]
                                        ]
                                    ]
                                ]
                            ],
                            '404' => [
                                'description' => 'کاربر یافت نشد',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/ErrorResponse']
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                '/api/auth/verify-otp' => [
                    'post' => [
                        'tags' => ['Authentication'],
                        'summary' => 'تأیید کد OTP',
                        'description' => 'کد OTP را تأیید می‌کند',
                        'requestBody' => [
                            'required' => true,
                            'content' => [
                                'application/json' => [
                                    'schema' => ['$ref' => '#/components/schemas/OtpVerifyRequest']
                                ]
                            ]
                        ],
                        'responses' => [
                            '200' => [
                                'description' => 'کد OTP تأیید شد',
                                'content' => [
                                    'application/json' => [
                                        'schema' => [
                                            'type' => 'object',
                                            'properties' => [
                                                'success' => ['type' => 'boolean', 'example' => true],
                                                'message' => ['type' => 'string', 'example' => 'کد تأیید صحیح است'],
                                                'data' => [
                                                    'type' => 'object',
                                                    'properties' => [
                                                        'access_token' => ['type' => 'string'],
                                                        'user' => ['$ref' => '#/components/schemas/User']
                                                    ]
                                                ]
                                            ]
                                        ]
                                    ]
                                ]
                            ],
                            '400' => [
                                'description' => 'کد OTP نامعتبر یا منقضی',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/ErrorResponse']
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                '/api/user/profile' => [
                    'get' => [
                        'tags' => ['User Profile'],
                        'summary' => 'دریافت پروفایل کاربر',
                        'description' => 'اطلاعات پروفایل کاربر را دریافت می‌کند',
                        'security' => [
                            ['bearerAuth' => []]
                        ],
                        'responses' => [
                            '200' => [
                                'description' => 'دریافت موفق',
                                'content' => [
                                    'application/json' => [
                                        'schema' => [
                                            'type' => 'object',
                                            'properties' => [
                                                'success' => ['type' => 'boolean', 'example' => true],
                                                'data' => ['$ref' => '#/components/schemas/User']
                                            ]
                                        ]
                                    ]
                                ]
                            ],
                            '401' => [
                                'description' => 'عدم احراز هویت',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/ErrorResponse']
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                '/api/user/profile/update' => [
                    'put' => [
                        'tags' => ['User Profile'],
                        'summary' => 'ویرایش پروفایل کاربر',
                        'description' => 'اطلاعات پروفایل کاربر را ویرایش می‌کند',
                        'security' => [
                            ['bearerAuth' => []]
                        ],
                        'requestBody' => [
                            'required' => true,
                            'content' => [
                                'application/json' => [
                                    'schema' => ['$ref' => '#/components/schemas/UpdateProfileRequest']
                                ],
                                'multipart/form-data' => [
                                    'schema' => ['$ref' => '#/components/schemas/UpdateProfileRequest']
                                ]
                            ]
                        ],
                        'responses' => [
                            '200' => [
                                'description' => 'ویرایش موفق',
                                'content' => [
                                    'application/json' => [
                                        'schema' => [
                                            'type' => 'object',
                                            'properties' => [
                                                'success' => ['type' => 'boolean', 'example' => true],
                                                'message' => ['type' => 'string'],
                                                'data' => ['$ref' => '#/components/schemas/User']
                                            ]
                                        ]
                                    ]
                                ]
                            ],
                            '401' => [
                                'description' => 'عدم احراز هویت',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/ErrorResponse']
                                    ]
                                ]
                            ],
                            '422' => [
                                'description' => 'داده‌های نامعتبر',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/ErrorResponse']
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                '/api/support/tickets' => [
                    'get' => [
                        'tags' => ['Support Tickets'],
                        'summary' => 'لیست تیکت‌های پشتیبانی',
                        'description' => 'لیست تمام تیکت‌های پشتیبانی کاربر را دریافت می‌کند',
                        'security' => [
                            ['bearerAuth' => []]
                        ],
                        'responses' => [
                            '200' => [
                                'description' => 'دریافت موفق',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/SuccessResponse']
                                    ]
                                ]
                            ],
                            '401' => [
                                'description' => 'عدم احراز هویت',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/ErrorResponse']
                                    ]
                                ]
                            ]
                        ]
                    ],
                    'post' => [
                        'tags' => ['Support Tickets'],
                        'summary' => 'ایجاد تیکت پشتیبانی جدید',
                        'description' => 'تیکت پشتیبانی جدید ایجاد می‌کند',
                        'security' => [
                            ['bearerAuth' => []]
                        ],
                        'requestBody' => [
                            'required' => true,
                            'content' => [
                                'application/json' => [
                                    'schema' => [
                                        'type' => 'object',
                                        'required' => ['title', 'description'],
                                        'properties' => [
                                            'title' => ['type' => 'string', 'example' => 'مشکل در ورود'],
                                            'description' => ['type' => 'string', 'example' => 'نمی‌تونم وارد حسابم بشم'],
                                            'priority' => ['type' => 'string', 'enum' => ['low', 'medium', 'high']]
                                        ]
                                    ]
                                ]
                            ]
                        ],
                        'responses' => [
                            '201' => [
                                'description' => 'ایجاد موفق',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/SuccessResponse']
                                    ]
                                ]
                            ],
                            '401' => [
                                'description' => 'عدم احراز هویت',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/ErrorResponse']
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                '/api/support/tickets/{id}' => [
                    'get' => [
                        'tags' => ['Support Tickets'],
                        'summary' => 'جزئیات تیکت پشتیبانی',
                        'description' => 'جزئیات یک تیکت پشتیبانی را دریافت می‌کند',
                        'security' => [
                            ['bearerAuth' => []]
                        ],
                        'parameters' => [
                            [
                                'name' => 'id',
                                'in' => 'path',
                                'required' => true,
                                'schema' => ['type' => 'integer']
                            ]
                        ],
                        'responses' => [
                            '200' => [
                                'description' => 'دریافت موفق',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/SuccessResponse']
                                    ]
                                ]
                            ],
                            '404' => [
                                'description' => 'تیکت یافت نشد',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/ErrorResponse']
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                '/api/support/tickets/{id}/reply' => [
                    'post' => [
                        'tags' => ['Support Tickets'],
                        'summary' => 'پاسخ به تیکت پشتیبانی',
                        'description' => 'پاسخی به یک تیکت پشتیبانی ارسال می‌کند',
                        'security' => [
                            ['bearerAuth' => []]
                        ],
                        'parameters' => [
                            [
                                'name' => 'id',
                                'in' => 'path',
                                'required' => true,
                                'schema' => ['type' => 'integer']
                            ]
                        ],
                        'requestBody' => [
                            'required' => true,
                            'content' => [
                                'application/json' => [
                                    'schema' => [
                                        'type' => 'object',
                                        'required' => ['message'],
                                        'properties' => [
                                            'message' => ['type' => 'string', 'example' => 'متشکرم، مشکل شما را بررسی می‌کنیم']
                                        ]
                                    ]
                                ]
                            ]
                        ],
                        'responses' => [
                            '201' => [
                                'description' => 'ارسال موفق',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/SuccessResponse']
                                    ]
                                ]
                            ],
                            '401' => [
                                'description' => 'عدم احراز هویت',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/ErrorResponse']
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                '/api/badges' => [
                    'get' => [
                        'tags' => ['Gamification'],
                        'summary' => 'لیست نشان‌ها و بج‌ها',
                        'description' => 'لیست تمام نشان‌های دستیافته شده توسط کاربر را دریافت می‌کند',
                        'security' => [
                            ['bearerAuth' => []]
                        ],
                        'responses' => [
                            '200' => [
                                'description' => 'دریافت موفق',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/SuccessResponse']
                                    ]
                                ]
                            ],
                            '401' => [
                                'description' => 'عدم احراز هویت',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/ErrorResponse']
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                '/api/packages' => [
                    'get' => [
                        'tags' => ['Commerce'],
                        'summary' => 'لیست پکیج‌های اشتراک',
                        'description' => 'لیست تمام پکیج‌های اشتراک موجود را دریافت می‌کند',
                        'responses' => [
                            '200' => [
                                'description' => 'دریافت موفق',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/SuccessResponse']
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                '/api/categories' => [
                    'get' => [
                        'tags' => ['Content'],
                        'summary' => 'لیست دسته‌بندی‌ها',
                        'description' => 'لیست تمام دسته‌بندی‌های محتوا را دریافت می‌کند',
                        'responses' => [
                            '200' => [
                                'description' => 'دریافت موفق',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/SuccessResponse']
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                '/api/contents' => [
                    'get' => [
                        'tags' => ['Content'],
                        'summary' => 'لیست محتواها',
                        'description' => 'لیست تمام محتواهای موجود را دریافت می‌کند',
                        'responses' => [
                            '200' => [
                                'description' => 'دریافت موفق',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/SuccessResponse']
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                '/api/contents/{id}' => [
                    'get' => [
                        'tags' => ['Content'],
                        'summary' => 'جزئیات محتوا',
                        'description' => 'جزئیات یک محتوا را دریافت می‌کند',
                        'parameters' => [
                            [
                                'name' => 'id',
                                'in' => 'path',
                                'required' => true,
                                'schema' => ['type' => 'integer']
                            ]
                        ],
                        'responses' => [
                            '200' => [
                                'description' => 'دریافت موفق',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/SuccessResponse']
                                    ]
                                ]
                            ],
                            '404' => [
                                'description' => 'محتوا یافت نشد',
                                'content' => [
                                    'application/json' => [
                                        'schema' => ['$ref' => '#/components/schemas/ErrorResponse']
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ];

        return response()->json($swagger);
    }
}
