import time
import threading
import requests
from app.config import SALARY_DETECT_URL, SALARY_DETECT_HEADERS, get_random_salary_payload
from app.utils.logger import logger

class SalaryDetect:
    def __init__(self):
        self._running = False
        self._thread = None

    def _run(self):
        while self._running:
            logger.info(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Detecting salary...")
            try:
                payload = get_random_salary_payload()
                response = requests.post(SALARY_DETECT_URL, headers=SALARY_DETECT_HEADERS, json=payload)
                logger.info(f"POST {SALARY_DETECT_URL} status: {response.status_code}, response: {response.text}")
            except Exception as e:
                logger.error(f"Error during POST: {e}")
            logger.info(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Salary detection complete")
            time.sleep(120)

    def start(self):
        if not self._running:
            self._running = True
            self._thread = threading.Thread(target=self._run, daemon=True)
            self._thread.start()

    def stop(self):
        self._running = False
        if self._thread:
            self._thread.join()


    
