import { createAction, Property } from "@activepieces/pieces-framework";
import Odoo from "../../commom/index";
import { odooAuth } from '../auth';

export default createAction({
    name: 'create_company', // Must be a unique across the piece, this shouldn't be changed.
    auth: odooAuth,
    displayName: 'Create company',
    description: 'Create/Update company on Odoo',
    audience: 'both',
    aiMetadata: { description: 'Upserts a company partner (res.partner with is_company set) in Odoo by name: it searches for a company with the same name, updating it if found or creating one otherwise, setting phone and email. Use to add or refresh a company record. Not idempotent in effect — the first call may create a record and matching is by exact name.', idempotent: false },
    props: {
        // Properties to ask from the user, in this ask we will take number of
        name: Property.ShortText({
            displayName: 'Company Name',
            description: undefined,
            required: true,
        }),
        phone: Property.ShortText({
            displayName: 'Company Phone',
            description: undefined,
            required: true,
        }),
        email: Property.ShortText({
            displayName: 'Company E-mail',
            description: undefined,
            required: true,
        })
    },
    async run(context) {
        const odoo = new Odoo({
            url: context.auth.props.base_url,
            port: 443,
            db: context.auth.props.database,
            username: context.auth.props.username,
            password: context.auth.props.api_key,
        })

        try {
            const uid = await odoo.connect();
            console.log("Connected to Odoo server. Uid: ", uid);
            const c = await odoo.saveCompany({
                name: context.propsValue['name'],
                phone: context.propsValue['phone'],
                email: context.propsValue['email'],
            });
            return `Company ${c} created!`
        } catch (err) {
            return err;
        }
    },
});

