from django.test import TestCase, Client
from django.shortcuts import get_object_or_404
from metronus_app.model.company import Company
from metronus_app.model.employee import Employee
from metronus_app.model.administrator import Administrator
from django.contrib.auth.models import User
from metronus_app.model.employeeLog import EmployeeLog
from metronus_app.model.project                       import Project
from metronus_app.model.department                    import Department
from metronus_app.model.role                          import Role
from metronus_app.model.projectDepartment             import ProjectDepartment
from metronus_app.model.projectDepartmentEmployeeRole import ProjectDepartmentEmployeeRole
from django.urls import reverse
from populate_database                  import populate_roles
import json

class EmployeeTestCase(TestCase):
    """This class provides a test case for employee management"""
    @classmethod
    def setUpTestData(cls):
        """
        Loads the data to the database for tests to be done
        """
        company1 = Company.objects.create(
            cif="123",
            company_name="company1",
            short_name="mplp",
            email="company1@gmail.com",
            phone="123456789"
        )

        company2 = Company.objects.create(
            cif="456",
            company_name="company2",
            short_name="lmao",
            email="company2@gmail.com",
            phone="1987654321"
        )

        admin_user = User.objects.create_user(
            username="admin1",
            password="123456",
            email="admin1@metronus.es",
            first_name="Pepito",
            last_name="Pérez"
        )

        # Admin
        Administrator.objects.create(
            user=admin_user,
            user_type="A",
            identifier="adm01",
            phone="666555444",
            company_id=company1
        )

        employee1_user = User.objects.create_user(
            username="emp1",
            password="123456",
            email="emp1@metronus.es",
            first_name="Álvaro",
            last_name="Varo"
        )

        employee2_user = User.objects.create_user(
            username="emp2",
            password="123456",
            email="emp2@metronus.es",
            first_name="Alberto",
            last_name="Berto"
        )
        employee3_user = User.objects.create_user(
            username="emp3",
            password="123456",
            email="emp3@metronus.es",
            first_name="Alberto",
            last_name="Bertoa"
        )
        employee4_user = User.objects.create_user(
            username="emp4",
            password="123456",
            email="emp4@metronus.es",
            first_name="Albertona",
            last_name="Bertona"
        )
        employee5_user = User.objects.create_user(
            username="emp5",
            password="123456",
            email="emp5@metronus.es",
            first_name="Alberna",
            last_name="Berta"
        )

        # Employee 1
        emp1=Employee.objects.create(
            user=employee1_user,
            user_type="E",
            identifier="emp01",
            phone="666555444",
            company_id=company1
        )
        # Employee 3
        emp3=Employee.objects.create(
            user=employee3_user,
            user_type="E",
            identifier="emp03",
            phone="666553444",
            company_id=company1
        )

        # Employee 2
        Employee.objects.create(
            user=employee2_user,
            user_type="E",
            identifier="emp02",
            phone="666555444",
            company_id=company2
        )
        # Employee 4
        emp4=Employee.objects.create(
            user=employee4_user,
            user_type="E",
            identifier="emp04",
            phone="666555444",
            company_id=company1
        )
        # Employee 4
        emp5=Employee.objects.create(
            user=employee5_user,
            user_type="E",
            identifier="emp05",
            phone="666555845",
            company_id=company1
        )
        proj1 = Project.objects.create(
            name="Metronus",
            de
leted=False,
            company_id=company1)
        proj2 = Project.objects.create(
                    name="Metronusu",
                    deleted=False,
                    company_id=company1)
        dep1 = Department.objects.create(
            name="Backend",
            active=True,company_id=company1)

        dep2 = Department.objects
.create(
            name="Frontend",
            active=True,
            company_id=company1)

        #Frontend
        pd1 = ProjectDepartment.objects.create(
            project_id = proj1,
            department_id = dep1)

        #Backend
        pd2 = ProjectDepartment.objects.create(
            project_id = proj1,
            department_id = dep2)
        pd3 = ProjectDepartment.objects.create(
            project_id = proj2,
            department_id = dep2)

        populate_roles()

        emp_role=Role.objects.get(name="EMPLOYEE")
        coor_role=Role.objects.get(name="COORDINATOR")
        pm_role=Role.objects.get(name="PROJECT_MANAGER")
        team_role=Role.objects.get(name="TEAM_MANAGER")

        ProjectDepartmentEmployeeRole.objects.create(
            projectDepartment_id=pd1,
            employee_id=emp1,
            role_id= emp_role)
        ProjectDepartmentEmployeeRole.objects.create(
            projectDepartment_id=pd1,
            employee_id=emp3,
            role_id=pm_role)

        ProjectDepartmentEmployeeRole.objects.create(
            projectDepartment_id=pd2,
            employee_id=emp4,
            role_id=coor_role)
        ProjectDepartmentEmployeeRole.objects.create(
            projectDepartment_id=pd3,
            employee_id=emp5,
            role_id=coor_role)

    def test_create_form_view(self):
        """ Logged in as an administrator, get the form view"""
        c = Client()
        c.login(username="admin1", password="123456")

        response = c.get("/employee/create")

        self.assertEquals(response.status_code, 200)
        self.assertTrue(response.context["form"] is not None)

    def test_create_employee_positive(self):
        """ Logged in as an administrator, try to create an employee"""
        c = Client()
        c.login(username="admin1", password="123456")

        logs_before = EmployeeLog.objects.all().count()

        response = c.post("/employee/create", {
            "username": "employee1",
            "password1": "ihatemyboss",
            "password2": "ihatemyboss",
            "first_name": "Francisco",
            "last_name": "Romualdo",
            "email": "frc@empresa.com",
            "identifier": "frc01",
            "phone": "654321987",
            "price_per_hour": "1.0"
        })

        self.assertRedirects(response, "/employee/view/employee1/", fetch_redirect_response=False)

        # Check that the employee has been successfully created

        employee = Employee.objects.get(identifier="frc01")
        self.assertEquals(employee.user_type, "E")
        self.assertEquals(employee.phone, "654321987")
        self.assertEquals(employee.company_id, Administrator.objects.get(identifier="adm01").company_id)
        self.assertEquals(employee.user.username, "employee1")
        self.assertEquals(employee.user.first_name, "Francisco")
        self.assertEquals(employee.user.last_name, "Romualdo")
        self.assertEquals(employee.user.email, "frc@empresa.com")

        logs_after = EmployeeLog.objects.all().count()

        self.assertEquals(logs_before + 1, logs_after)

    def test_create_employee_with_redirect_positive(self):
        """ Logged in as an administrator, try to create an employee"""
        c = Client()
        c.login(username="admin1", password="123456")

        logs_before = EmployeeLog.objects.all().count()

        response = c.post("/employee/create?redirect=true", {
            "username": "employee11343154",
            "password1": "ihatemyboss",
            "password2": "ihatemyboss",
          