<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\ResponseInterface;
use App\Services\LojaService;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\Validations\LojaValidation;
use App\Validations\LojaProdutoValidation;
use App\Services\LojaProdutoService;
use App\Services\LojaCompraService;
use App\Services\ServiceResponse\ServiceResponse;

class LojaController extends BaseController {

	public function registerLoja(): Response {

		$dados = $this->getDataPost();

		$validar = array ();
		$validar = LojaValidation::register( $dados );
		if (count( $validar ) > 0) {
			return $this->response->setStatusCode( 400 )
				->setJSON( [ 
				'message' => 'Informe os dados corretamente.',
				'erros' => $validar
			] );
		}

		$service = new LojaService();

		$response_service = $service->registerLoja( $dados );

		$response_messages = [ 
			ServiceResponseInterface::DATABASE_CONNECTION_ERROR => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro de conexão com o banco de dados.'
			],
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Loja cadastrada com sucesso.'
			],
			ServiceResponseInterface::INSUFFICIENT_DATA => [ 
				'code' => ResponseInterface::HTTP_BAD_REQUEST,
				'message' => 'Informe os dados corretamente.'
			],
			ServiceResponseInterface::HAVE_CONFLICTS => [ 
				'code' => ResponseInterface::HTTP_CONFLICT,
				'message' => 'Loja já exite para a unidade.'
			]
		];
		return $this->getResponse( $response_service, $response_messages );

	}

	public function changeLojaStatus(): Response {

		$dados = $this->getDataPost();

		$validar = array ();
		$validar = LojaValidation::changestatus( $dados );
		if (count( $validar ) > 0) {
			return $this->response->setStatusCode( 400 )
				->setJSON( [ 
				'message' => 'Informe os dados corretamente.',
				'erros' => $validar
			] );
		}

		$service = new LojaService();

		$response_service = $service->changeLojaStatus( $dados );

		$response_messages = [ 
			ServiceResponseInterface::DATABASE_CONNECTION_ERROR => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro de conexão com o banco de dados.'
			],
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'alteração realizada com sucesso.'
			],
			ServiceResponseInterface::INSUFFICIENT_DATA => [ 
				'code' => ResponseInterface::HTTP_BAD_REQUEST,
				'message' => 'Informe os dados corretamente.'
			]
		];
		return $this->getResponse( $response_service, $response_messages );

	}

	public function getLojas(): Response {

		$service = new LojaService();

		$response_service = $service->getLojas();

		$response_messages = [ 
			ServiceResponseInterface::DATABASE_CONNECTION_ERROR => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro de conexão com o banco de dados.'
			],
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'alteração realizada com sucesso.'
			],
			ServiceResponseInterface::INSUFFICIENT_DATA => [ 
				'code' => ResponseInterface::HTTP_BAD_REQUEST,
				'message' => 'Informe os dados corretamente.'
			],
			ServiceResponseInterface::RECORD_NOT_FOUND => [ 
				'code' => ResponseInterface::HTTP_NO_CONTENT,
				'message' => 'Nenhim registro encntrado.'
			]
		];
		return $this->getResponse( $response_service, $response_messages );

	}

	public function getDetalheLoja(int $id_loja): Response {


	}

	public function updateLoja(int $id_loja): Response {


	}

	public function addProduto(): Response {

		$dados = $this->getDataPost();

		$validar = array ();
		$validar = LojaProdutoValidation::addProduto( $dados );
		if (count( $validar ) > 0) {
			return $this->response->setStatusCode( 400 )
				->setJSON( [ 
				'message' => 'Informe os dados corretamente.',
				'erros' => $validar
			] );
		}

		$service = new LojaProdutoService();

		$response_service = $service->addProduto( $dados );

		$response_messages = [ 
			ServiceResponseInterface::DATABASE_CONNECTION_ERROR => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro de conexão com o banco de dados.'
			],
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Produto cadastrada com sucesso.'
			],
			ServiceResponseInterface::INSUFFICIENT_DATA => [ 
				'code' => ResponseInterface::HTTP_BAD_REQUEST,
				'message' => 'Informe os dados corretamente.'
			],
			ServiceResponseInterface::HAVE_CONFLICTS => [ 
				'code' => ResponseInterface::HTTP_CONFLICT,
				'message' => 'Produto já exite para a essa loja.'
			]
		];
		return $this->getResponse( $response_service, $response_messages );

	}

	public function changeLojaProdutoStatus(): Response {

		$dados = $this->getDataPost();

		$validar = array ();
		$validar = LojaProdutoValidation::changestatus( $dados );
		if (count( $validar ) > 0) {
			return $this->response->setStatusCode( 400 )
				->setJSON( [ 
				'message' => 'Informe os dados corretamente.',
				'erros' => $validar
			] );
		}

		$service = new LojaProdutoService();

		$response_service = $service->changeLojaProdutoStatus( $dados );

		$response_messages = [ 
			ServiceResponseInterface::DATABASE_CONNECTION_ERROR => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro de conexão com o banco de dados.'
			],
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Alteração de status realizada com sucesso.'
			],
			ServiceResponseInterface::INSUFFICIENT_DATA => [ 
				'code' => ResponseInterface::HTTP_BAD_REQUEST,
				'message' => 'Informe os dados corretamente.'
			],
			ServiceResponseInterface::HAVE_CONFLICTS => [ 
				'code' => ResponseInterface::HTTP_CONFLICT,
				'message' => 'Dados conflitantes.'
			],
			ServiceResponseInterface::RECORD_NOT_FOUND => [ 
				'code' => ResponseInterface::HTTP_NO_CONTENT,
				'message' => 'Não Foram encontrados registros.'
			]
		];
		return $this->getResponse( $response_service, $response_messages );

	}

	public function getLojaProdutos(int $id_loja, int $id_status): Response {

		$service = new LojaProdutoService();

		$response_service = $service->getLojaProdutos( $id_loja, $id_status );

		$response_messages = [ 
			ServiceResponseInterface::DATABASE_CONNECTION_ERROR => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro de conexão com o banco de dados.'
			],
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Pesquisa realizada com sucesso.'
			],
			ServiceResponseInterface::INSUFFICIENT_DATA => [ 
				'code' => ResponseInterface::HTTP_BAD_REQUEST,
				'message' => 'Informe os dados corretamente.'
			],
			ServiceResponseInterface::RECORD_NOT_FOUND => [ 
				'code' => ResponseInterface::HTTP_NO_CONTENT,
				'message' => 'Não Foram encontrados registros.'
			]
		];
		return $this->getResponse( $response_service, $response_messages );

	}

	public function getLojaCompras(int $id_loja, int $id_status_compra): Response {

		$service = new LojaService();

		$compras_service = $service->getComprasByLojaId( $id_loja, $id_status_compra );

		$array_ids = $service->getIdsCompras( $compras_service->getData() ['produtos'] );

		$response_pets = $service->getPetsByComprasId( $array_ids );

		$service_response = new ServiceResponse();

		$service_response->setStatus( $compras_service->getStatus() );
		$service_response->setData( [ 
			$compras_service->getData(),
			$response_pets->getData()
		] );

		$response_messages = [ 
			ServiceResponseInterface::DATABASE_CONNECTION_ERROR => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro de conexão com o banco de dados.'
			],
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Pesquisa realizada com sucesso.'
			],
			ServiceResponseInterface::INSUFFICIENT_DATA => [ 
				'code' => ResponseInterface::HTTP_BAD_REQUEST,
				'message' => 'Informe os dados corretamente.'
			],
			ServiceResponseInterface::RECORD_NOT_FOUND => [ 
				'code' => ResponseInterface::HTTP_NO_CONTENT,
				'message' => 'Não Foram encontrados registros.'
			]
		];
		return $this->getResponse( $service_response, $response_messages );

	}

	public function getComprasItensById(int $id_loja_compra): Response {

		$service = new LojaCompraService();

		$response_messages = [ 
			ServiceResponseInterface::DATABASE_CONNECTION_ERROR => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro de conexão com o banco de dados.'
			],
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Pesquisa realizada com sucesso.'
			],
			ServiceResponseInterface::INSUFFICIENT_DATA => [ 
				'code' => ResponseInterface::HTTP_BAD_REQUEST,
				'message' => 'Informe os dados corretamente.'
			],
			ServiceResponseInterface::RECORD_NOT_FOUND => [ 
				'code' => ResponseInterface::HTTP_NO_CONTENT,
				'message' => 'Não Foram encontrados registros.'
			]
		];

		$response_service = $service->getComprasItensById( $id_loja_compra );
		if ($response_service->getStatus() != ServiceResponseInterface::SUCESS) {
			return $this->getResponse( $response_service, $response_messages );
		}
		$array_ids = $service->getIdsComprasFromLojaCompraItem( $response_service->getData() ['produtos'] );
		$response_pets = $service->getPetsByComprasId( $array_ids );
		$service_response = new ServiceResponse();

		$service_response->setStatus( $response_service->getStatus() );
		$service_response->setData( [ 
			$response_service->getData(),
			$response_pets->getData()
		] );
		return $this->getResponse( $service_response, $response_messages );

	}

	public function getComprasByUsuarioId(int $idusuario, int $idstatus): Response {

		$service = new LojaService();
		$service_response = $service->getComprasByUsuarioId( $idusuario, $idstatus );

		$response_messages = [ 
			ServiceResponseInterface::DATABASE_CONNECTION_ERROR => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro de conexão com o banco de dados.'
			],
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Pesquisa realizada com sucesso.'
			],
			ServiceResponseInterface::INSUFFICIENT_DATA => [ 
				'code' => ResponseInterface::HTTP_BAD_REQUEST,
				'message' => 'Informe os dados corretamente.'
			],
			ServiceResponseInterface::RECORD_NOT_FOUND => [ 
				'code' => ResponseInterface::HTTP_NO_CONTENT,
				'message' => 'Não Foram encontrados registros.'
			]
		];
		return $this->getResponse( $service_response, $response_messages );

	}

}

