<?php

namespace Offy\Bundle\DashboardBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations\View;
use Offy\Bundle\DashboardBundle\Document\OpenPosition;
use Offy\Bundle\DashboardBundle\Document\Job;
use Offy\Bundle\DashboardBundle\Document\Stage;
use Offy\Bundle\DashboardBundle\Document\Applicant;

class ApplicantController extends Controller {

    /**
     * 
     * @return array
     * @View()
     */
    public function getApplicantAction() {
        $dm = $this->get('doctrine_mongodb')->getManager();
        $stages = $dm->getRepository('OffyDashboardBundle:Stage')->findAll();
        $stageArray = array();
        foreach ($stages as $stage) {
            $stageObj = new Stage();
            $stageObj = $stage;
            $stageId = $stageObj->getStageId();

            $stageArray[$stageId] = array(
                "stage_id" => $stageObj->getStageId(),
                "name" => $stageObj->getName(),
                "order" => $stageObj->getOrder(),
                "number" => 0
            );
        }
        
        $repository = $dm->getRepository('OffyDashboardBundle:Job');
        $jobs = $repository->findAll();
        $results = array();
        foreach ($jobs as $job) {
            $jobObj = new Job();
            $jobObj = $job;
            $job_id = $jobObj->getJobId();
            $jobArray = array();
            $jobArray["job_id"] = $jobObj->getJobId();
            $jobArray["job_name"] = $jobObj->getName();
            $jobArray["department_id"] = $jobObj->getDepartmentId();
            $jobArray["department_name"] = $jobObj->getDepartmentName();
            $jobArray["image"] = $jobObj->getImage();
            $jobArray["child"] = $stageArray;
            
            $applicantStage = $dm->getRepository('OffyDashboardBundle:Applicant')->findBy(array("job_id" => $job_id));
            foreach ($applicantStage as $applicant) {
                $applicantObj = new Applicant();
                $applicantObj = $applicant;
                $stage_id = $applicantObj->getStageId();
                $jobArray["child"][$stage_id]["number"] ++;
            }
            $stageList = $jobArray["child"];
            $stageData = array();
            foreach ($stageList as $stage)
            {
                $stageData[] = $stage;
            }
            $jobArray["child"] = $stageData;
            $results[] = $jobArray;
        }
        return array("results" => true, "error" => "ok", "data" => $results);
    }

}
