<?xml version="1.0" encoding="UTF-8"?>

<!--

 This file is part of the Sylius package.

 (c) Sylius Sp. z o.o.

 For the full copyright and license information, please view the LICENSE
 file that was distributed with this source code.

-->

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                                      http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <mapped-superclass name="Sylius\Component\Core\Model\Product" table="sylius_product">

        <field name="variantSelectionMethod" column="variant_selection_method" type="string" nullable="false" />
        <field name="averageRating" column="average_rating" type="float">
            <options>
                <option name="default">0</option>
            </options>
        </field>

        <one-to-many field="productTaxons" target-entity="Sylius\Component\Core\Model\ProductTaxonInterface" mapped-by="product" orphan-removal="true">
            <order-by>
                <order-by-field name="id" />
            </order-by>

            <cascade>
                <cascade-all/>
            </cascade>
        </one-to-many>

        <one-to-many field="reviews" target-entity="Sylius\Component\Review\Model\ReviewInterface" mapped-by="reviewSubject">
            <cascade>
                <cascade-all />
            </cascade>
        </one-to-many>

        <many-to-many field="channels" target-entity="Sylius\Component\Channel\Model\ChannelInterface">
            <order-by>
                <order-by-field name="id" />
            </order-by>

            <join-table name="sylius_product_channels">
                <join-columns>
                    <join-column name="product_id" referenced-column-name="id" nullable="false" on-delete="CASCADE" />
                </join-columns>
                <inverse-join-columns>
                    <join-column name="channel_id" referenced-column-name="id" nullable="false" on-delete="CASCADE" />
                </inverse-join-columns>
            </join-table>
        </many-to-many>

        <many-to-one field="mainTaxon" target-entity="Sylius\Component\Taxonomy\Model\TaxonInterface">
            <join-column name="main_taxon_id" referenced-column-name="id" nullable="true" on-delete="SET NULL" />
        </many-to-one>

        <one-to-many field="images" target-entity="Sylius\Component\Core\Model\ProductImageInterface" mapped-by="owner" orphan-removal="true">
            <order-by>
                <order-by-field name="id" />
            </order-by>

            <cascade>
                <cascade-all/>
            </cascade>
        </one-to-many>
    </mapped-superclass>

</doctrine-mapping>
