<?php

namespace App\Http\Controllers\Realty;

use App\Enums\SeveritySystemLog;
use App\Http\Controllers\Controller;
use App\Http\Messages\ErrorMessages;
use App\Http\Messages\SuccessMessages;
use App\Http\Requests\PaginationRequest;
use App\Http\Requests\Realty\ProjectRequest;
use App\Http\Resources\PaginacionResource;
use App\Http\Resources\Realty\ProjectResource;
use App\Http\Responses\ApiResponse;
use App\Services\Project\ProjectService as ProjectProjectService;
use App\Services\SystemLogService;

class ProjectController extends Controller
{
    protected $projectService;
    protected $systemLogService;

    public function __construct(ProjectProjectService $projectService, SystemLogService $systemLogService )
    {
        $this->projectService = $projectService;
        $this->systemLogService = $systemLogService;
    }

    /**
     * @OA\Post(
     *     path="/api/project",
     *     summary="Register to Project",
     *     tags={"Project"},
     *     security={{ "bearerAuth": {} }},
     *     @OA\RequestBody(
     *         required=true,
     *         @OA\JsonContent(
     *             @OA\Property(property="title", type="string", maxLength=255),
     *             @OA\Property(property="description", type="string"),
     *             @OA\Property(property="num_pisos"),
     *         )
     *     ),
     *     @OA\Response(response=200, description="Successful operation"),
     *     @OA\Response(response=400, description="Invalid request")
     * )
     */

    public function register(ProjectRequest $project_request)
    {
        try {
            $project = $this->projectService->createProject($project_request->all());
            $this->systemLogService->logActivity('proyecto','Proyecto registrado', SeveritySystemLog::info->name, $project);
            return ApiResponse::success(SuccessMessages::CREATE_SUCCESS, new ProjectResource($project), [], 201);
        } catch (\Exception $e) {
            $this->systemLogService->logActivity('proyecto','Registro de Proyecto fallido' , SeveritySystemLog::error->name);
            return ApiResponse::error(ErrorMessages::BAD_REQUEST, $e->getMessage(), [], $e->getCode());
        }
    }

    /**
     * @OA\Put(
     *     path="/api/project/{id}",
     *     summary="Editar to Project",
     *     tags={"Project"},
     *     security={{ "bearerAuth": {} }},
     *     @OA\Parameter(
     *         name="id",
     *         in="path",
     *         description="ID of the project to update",
     *         required=true,
     *         @OA\Schema(type="integer")
     *     ),
     *     @OA\RequestBody(
     *         required=true,
     *         @OA\JsonContent(
     *             @OA\Property(property="title", type="string", maxLength=255),
     *             @OA\Property(property="description", type="string"),
     *             @OA\Property(property="num_pisos", example=5),
     *         )
     *     ),
     *     @OA\Response(response=200, description="Successful operation"),
     *     @OA\Response(response=400, description="Invalid request")
     * )
     */

    public function update(ProjectRequest $project_request, $id)
    {
        try {
            $project = $this->projectService->updateProject($id, $project_request->all());
            $this->systemLogService->logActivity('proyecto','Proyecto actualizado', SeveritySystemLog::info->name, $project);
            return ApiResponse::success(SuccessMessages::UPDATE_SUCCESS, new ProjectResource($project), [], 200);
        } catch (\Exception $e) {
            $this->systemLogService->logActivity('proyecto','Actualización de proyecto fallida' , SeveritySystemLog::error->name);
            return ApiResponse::error(ErrorMessages::BAD_REQUEST, $e->getMessage(), [], 500);
        }
    }

    /**
     * @OA\Get(
     *     path="/api/project/{id}",
     *     summary="Get user by ID",
     *     tags={"Project"},
     *     security={{ "bearerAuth": {} }},
     *     @OA\Parameter(
     *         name="id",
     *         in="path",
     *         description="ID of the project",
     *         required=true,
     *         @OA\Schema(
     *             type="integer",
     *             format="int64"
     *         )
     *     ),
     *     @OA\Response(response=200, description="Successful operation"),
     *     @OA\Response(response=404, description="Customer not found"),
     *     @OA\Response(response=500, description="Internal server error"),
     * )
     */

    public function get($id)
    {
        try {
            $project = $this->projectService->getProjectbyId($id);
            return ApiResponse::success(SuccessMessages::SUCCESSFUL, new ProjectResource($project), [], 200);
        } catch (\Exception $e) {
            return ApiResponse::error(ErrorMessages::BAD_REQUEST, $e->getMessage(), [], $e->getCode());
        }
    }

    /**
     * @OA\Get(
     *     path="/api/projects",
     *     summary="List project with pagination",
     *     tags={"Project"},
     *     @OA\Parameter(
     *         name="search",
     *         in="query",
     *         description="Search query",
     *         required=false,
     *         @OA\Schema(type="string", maxLength=255)
     *     ),
     *     @OA\Parameter(
     *         name="itemsPerPage",
     *         in="query",
     *         description="Items per page",
     *         required=true,
     *         @OA\Schema(type="integer", minimum=1)
     *     ),
     *     @OA\Parameter(
     *         name="page",
     *         in="query",
     *         description="Page number",
     *         required=true,
     *         @OA\Schema(type="integer", minimum=1)
     *     ),
     *     @OA\Parameter(
     *         name="sortBy",
     *         in="query",
     *         description="Sort by field",
     *         required=false,
     *         @OA\Schema(type="string", maxLength=255)
     *     ),
     *     @OA\Parameter(
     *         name="orderBy",
     *         in="query",
     *         description="Sort order",
     *         required=false,
     *         @OA\Schema(type="string", enum={"asc", "desc"})
     *     ),
     *     @OA\Response(response=200, description="Successful operation"),
     *     @OA\Response(response=500, description="Internal server error")
     * )
     */

    public function list_project_pagination(PaginationRequest $pagerequest)
    {
        try {
            $projects = $this->projectService->getAllProjectpagination($pagerequest);
            $projects->data = ProjectResource::collection($projects->getCollection());
            return ApiResponse::success(SuccessMessages::SUCCESSFUL, new PaginacionResource($projects), [], 200);
        } catch (\Exception $e) {
            return ApiResponse::error(ErrorMessages::BAD_REQUEST, $e->getMessage(), [], 500);
        }
    }

    /**
     * @OA\Get(
     *     path="/projects/combo",
     *     summary="Get all project",
     *     tags={"Project"},
     *     @OA\Response(response=200, description="Successful operation"),
     *     @OA\Response(response=404, description="Customer not found"),
     *     @OA\Response(response=500, description="Internal server error"),
     * )
    */
     public function all_projects()
     {
         try {
             $projects = $this->projectService->getAllProject();

             return ApiResponse::success(SuccessMessages::SUCCESSFUL, ProjectResource::collection($projects), [], 200);
         } catch (\Exception $e) {
             return ApiResponse::error(ErrorMessages::BAD_REQUEST, $e->getMessage(), [], $e->getCode());
         }
     }


}
