import psycopg2
import os
from psycopg2.extras import RealDictCursor

class Database:
    """Initialization."""

    def __init__(self):
    	self.db_name = os.getenv('DB_NAME')
    	self.db_host = os.getenv('DB_HOST')
    	self.db_user = os.getenv('DB_USER')
    	self.db_password = os.getenv('DB_PASSWORD')
    	self.conn = psycopg2.connect(database=self.db_name,host=self.db_host,user=self.db_user,password=self.db_password)
    	self.curr = self.conn.cursor(cursor_factory=RealDictCursor)

    def create_table(self):
        """Create tables."""

        queries = [
        	"""
	        CREATE TABLE IF NOT EXISTS users(
				user_id serial PRIMARY KEY,
				firstname varchar NOT NULL,
                lastname varchar NOT NULL,
                othername varchar NOT NULL,
				email varchar NOT NULL,
                password varchar NOT NULL,
				phoneNumber varchar NOT NULL,
                passportUrl varchar NOT NULL,
                role varchar NOT NULL,
                date TIMESTAMP
			)"""
        ]
        try:
            for query in queries:
            	self.curr.execute(query)
            self.conn.commit()
            self.curr.close()
        except Exception as e:
            return e

    def destroy_table(self):
        """Destroy tables"""

        users = "DROP TABLE IF EXISTS  users CASCADE"
        queries = [users]
        try:
            for query in queries:
                self.curr.execute(query)
            self.conn.commit()
            self.curr.close()
        except Exception as e:
            return e