# -*- coding: utf-8 -*-
from odoo import http
from odoo.http import request

from odoo.addons.auth_signup_confirmation.controllers.auth_signup_confirmation import (
    AuthConfirm,
)


class AuthLead(AuthConfirm):
    @http.route("/web/signup/confirm", type="http", auth="public")
    def singnup_using_generated_link(self, *args, **kw):
        user_state_before = (
            request.env["res.users"]
            .sudo()
            .search([("partner_id.signup_token", "=", kw["token"])])
            .active
        )
        res = super(AuthLead, self).singnup_using_generated_link(*args, **kw)
        user = (
            request.env["res.users"]
            .sudo()
            .search([("partner_id.signup_token", "=", kw["token"])])
        )
        if user.active and not user_state_before:
            request.env["crm.lead"].sudo().create(
                {
                    "name": user.partner_id.name,
                    "partner_id": user.partner_id.id,
                    "contact_name": user.partner_id.name,
                }
            )
        return res
