from datetime import datetime
from models.financial_information import FinancialInformationPage


class FinancialInformationPageService:
    @staticmethod
    async def insert_new_record(db, current_user, **kwargs):
        """
        Insert a new record into the financial_information_pages table using MySQLDB's execute_insert.

        :param db: An instance of MySQLDB
        :param kwargs: Dictionary of column values for the new record
        """
        new_record = FinancialInformationPage(
            **kwargs,
            created_at=datetime.now(),
            created_by=current_user.user_id,
        )
        new_record.update_modify(current_user.user_id)
        record = await db.execute_insert(new_record)
        return record

    @staticmethod
    async def get_all_records(db):
        """
        Get all records from the financial_information_pages table.
        """
        return await db.execute_query(FinancialInformationPage)

    @staticmethod
    async def get_record_by_id(db, page_id):
        """
        Get a record from the financial_information_pages table by id.
        """
        filters = [FinancialInformationPage.id == page_id]
        return await db.execute_query(FinancialInformationPage, filters=filters)

    @staticmethod
    async def delete_record_by_id(db, page_id):
        """
        Delete a record from the financial_information_pages table by id.
        """
        filters = [FinancialInformationPage.id == page_id]
        return await db.delete_record(
            FinancialInformationPage, record_id=page_id, filters=filters
        )

    @staticmethod
    async def get_all_records_for_project(db, project_id):
        """
        Get all records from the financial_information_pages table for a project.
        """
        filters = [
            FinancialInformationPage.project_id == project_id,
            FinancialInformationPage.is_active == True,
        ]
        return await db.execute_query(FinancialInformationPage, filters=filters)

    @staticmethod
    async def update_record_by_id(db, page_id, current_user, **kwargs):
        """
        Update a record in the financial_information_pages table by id.

        :param db: An instance of MySQLDB
        :param page_id: The ID of the record to update
        :param kwargs: Dictionary of column values to update
        """
        # Ensure 'modified_at' is set to the current timestamp
        kwargs["modified_at"] = datetime.now()
        kwargs["modified_by"] = current_user.user_id

        filters = [FinancialInformationPage.id == page_id]
        return await db.update_record(
            FinancialInformationPage, filters=filters, values=kwargs
        )
