<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="urn:magento:module:Mirasvit_ReportApi:etc/mst_report.xsd">
    <table name="mst_feed_report">
        <pk name="row_id" label="Row"/>
        <fk name="product_id" label="Product" table="catalog_product_entity"/>
        <fk name="order_id" label="Order" table="sales_order"/>

        <column name="created_at" label="Date" type="date"/>

        <column name="is_click" label="Number of Clicks" type="number"/>

        <column name="unique_orders" label="Number of Orders" fields="order_id" type="number"
                expr="COUNT(DISTINCT(%1))"/>

        <column name="subtotal" label="Revenue" type="money"/>

        <column name="subtotal_per_click" label="Revenue per Click" fields="is_click,subtotal" type="money"
                expr="SUM(%2) / SUM(%1)"/>

        <column name="feed_id" label="Feed" type="select" options="Mirasvit\Feed\Model\ResourceModel\Feed\Collection"/>
    </table>

    <table name="mst_feed_feed">
        <pk name="feed_id" label="Feed"/>

        <column name="name" label="Feed" />
    </table>

    <!--
        The MANY side goes on the left: `type` is read from whichever side the join is walked from
        (Relation::getType() returns $type[0] from the left table and $type[1] from the right), so
        "1n" means the left table reaches ONE row and the right reaches MANY. Declared the other way
        round, the Report API believed one report row had many feeds - and pushed a Feed column
        through a temporary table instead of joining it. This matches what the loader builds for
        every <fk>, where leftTable is always the referencing table.
    -->
    <relation name="mst_feed_feed-mst_feed_report" type="1n">
        <leftTable>mst_feed_report</leftTable>
        <rightTable>mst_feed_feed</rightTable>
        <condition>%1.feed_id=%2.feed_id</condition>
    </relation>
</config>