<?php

/**
 * Freeform for Craft CMS.
 *
 * @author        Solspace, Inc.
 * @copyright     Copyright (c) 2008-2026, Solspace, Inc.
 *
 * @see           https://docs.solspace.com/craft/freeform
 *
 * @license       https://docs.solspace.com/license-agreement
 */

namespace Solspace\Freeform\Fields\Traits;

use Solspace\Freeform\Attributes\Property\DefaultValue;
use Solspace\Freeform\Attributes\Property\Flag;
use Solspace\Freeform\Attributes\Property\Input;
use Solspace\Freeform\Attributes\Property\Limitation;
use Solspace\Freeform\Attributes\Property\Translatable;
use Solspace\Freeform\Attributes\Property\ValueGenerator;
use Solspace\Freeform\Fields\Implementations\Options\AssetSourceOptions;
use Solspace\Freeform\Fields\Implementations\ValueGenerators\AssetSourceGenerator;
use Solspace\Freeform\Fields\Interfaces\FileUploadInterface;

trait FileUploadTrait
{
    #[Flag(FileUploadInterface::FLAG_GLOBAL_PROPERTY)]
    #[Translatable]
    #[ValueGenerator(AssetSourceGenerator::class)]
    #[Limitation('props.file', 'source')]
    #[DefaultValue('props.file.source')]
    #[Input\Select(
        label: 'Asset Source',
        instructions: 'Select an asset source to be able to store user uploaded files.',
        order: 1,
        options: AssetSourceOptions::class,
    )]
    protected ?int $assetSourceId = null;

    #[Flag(FileUploadInterface::FLAG_GLOBAL_PROPERTY)]
    #[Translatable]
    #[Limitation('props.file', 'uploadLocation')]
    #[DefaultValue('props.file.uploadLocation')]
    #[Input\Text(
        label: 'Upload Location',
        instructions: 'The subfolder path that files should be uploaded to. May contain `{{ form.handle }}` or `{{ form.id }}` variables as well.',
        order: 2,
        placeholder: 'path/to/subfolder',
    )]
    protected ?string $defaultUploadLocation = null;

    public function getAssetSourceId(): ?int
    {
        return $this->getTranslationTable()->get('assetSourceId', $this->assetSourceId);
    }

    public function getDefaultUploadLocation(): ?string
    {
        return $this->getTranslationTable()->get('defaultUploadLocation', $this->defaultUploadLocation);
    }
}
