import rstr
from faker import Faker
from import_export import fields
from import_export.widgets import ForeignKeyWidget
from wbcore.contrib.io.resources import FilterModelResource
from wbfdm.models import Instrument
from wbportfolio.models import Trade

fake = Faker()


class TradeProposalTradeResource(FilterModelResource):
    """
    Trade Resource class to use to import trade from the trade proposal
    """

    DUMMY_FIELD_MAP = {
        "underlying_instrument": lambda: rstr.xeger("([A-Z]{2}[A-Z0-9]{9}[0-9]{1})"),
        "weighting": 1.0,
        "shares": 1000.2536,
        "comment": lambda: fake.sentence(),
        "order": 1,
    }
    underlying_instrument = fields.Field(
        column_name="underlying_instrument",
        attribute="underlying_instrument",
        widget=ForeignKeyWidget(Instrument, field="isin"),
    )

    class Meta:
        import_id_fields = ("id",)
        fields = (
            "id",
            "underlying_instrument",
            "weighting",
            "shares",
            "comment",
            "order",
        )
        export_order = fields
        model = Trade
