<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use OpenApi\Attributes as OA;

class CustomerController extends Controller
{
    #[OA\Get(
        path: '/api/customer/profile',
        operationId: 'customerProfile',
        summary: 'Get authenticated customer profile',
        description: 'Returns the full profile of the currently authenticated user including wallet information.',
        security: [['passport' => []]],
        tags: ['Customer'],
        responses: [
            new OA\Response(response: 200, description: 'Customer profile',
                content: new OA\JsonContent(properties: [
                    new OA\Property(property: 'data', ref: '#/components/schemas/UserResource'),
                ])),
            new OA\Response(response: 401, description: 'Unauthenticated',
                content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedError')),
        ]
    )]
    public function profile(Request $request)
    {
        return response()->json(['data' => $request->user()->load('wallet')]);
    }

    #[OA\Post(
        path: '/api/customer/profile',
        operationId: 'customerUpdateProfile',
        summary: 'Update customer profile',
        description: 'Updates the authenticated customer\'s name, email, or phone.',
        security: [['passport' => []]],
        tags: ['Customer'],
        requestBody: new OA\RequestBody(
            required: true,
            content: new OA\JsonContent(
                properties: [
                    new OA\Property(property: 'name',  type: 'string',  example: 'Ronio Updated'),
                    new OA\Property(property: 'email', type: 'string',  format: 'email', example: 'ronio2@example.com'),
                    new OA\Property(property: 'phone', type: 'string',  example: '+2348099998888'),
                ]
            )
        ),
        responses: [
            new OA\Response(response: 200, description: 'Profile updated',
                content: new OA\JsonContent(properties: [
                    new OA\Property(property: 'message', type: 'string', example: 'Profile updated.'),
                    new OA\Property(property: 'data',    ref: '#/components/schemas/UserResource'),
                ])),
            new OA\Response(response: 422, description: 'Validation error',
                content: new OA\JsonContent(ref: '#/components/schemas/ValidationError')),
            new OA\Response(response: 401, description: 'Unauthenticated',
                content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedError')),
        ]
    )]
    public function updateProfile(Request $request)
    {
        $data = $request->validate([
            'name'  => 'sometimes|string|max:255',
            'email' => 'sometimes|email|unique:users,email,' . $request->user()->id,
            'phone' => 'sometimes|string|unique:users,phone,' . $request->user()->id,
        ]);

        $request->user()->update($data);
        return response()->json(['message' => 'Profile updated.', 'data' => $request->user()->fresh()]);
    }

    #[OA\Get(
        path: '/api/customer/orders',
        operationId: 'customerOrders',
        summary: 'List customer orders',
        description: 'Returns a paginated list of all orders placed by the authenticated customer.',
        security: [['passport' => []]],
        tags: ['Customer'],
        parameters: [
            new OA\Parameter(name: 'page', in: 'query', required: false,
                schema: new OA\Schema(type: 'integer', default: 1)),
        ],
        responses: [
            new OA\Response(response: 200, description: 'Paginated order list',
                content: new OA\JsonContent(properties: [
                    new OA\Property(property: 'data', type: 'object',
                        properties: [
                            new OA\Property(property: 'data', type: 'array',
                                items: new OA\Items(ref: '#/components/schemas/OrderResource')),
                            new OA\Property(property: 'meta', ref: '#/components/schemas/PaginationMeta'),
                        ]),
                ])),
            new OA\Response(response: 401, description: 'Unauthenticated',
                content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedError')),
        ]
    )]
    public function orders(Request $request)
    {
        $orders = $request->user()->orders()->with('vendor', 'items')->latest()->paginate(20);
        return response()->json(['data' => $orders]);
    }
}
