1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
use super::*;
use fmt::Debug;
use once_cell::sync::Lazy;
use query_structure::{prelude::ParentContainer, DefaultKind};
use std::{borrow::Cow, boxed::Box, fmt};

type InputObjectFields<'a> =
    Option<Arc<Lazy<Vec<InputField<'a>>, Box<dyn FnOnce() -> Vec<InputField<'a>> + Send + Sync + 'a>>>>;

#[derive(Clone)]
pub struct InputObjectType<'a> {
    pub identifier: Identifier,
    pub constraints: InputObjectTypeConstraints<'a>,
    pub(crate) fields: InputObjectFields<'a>,
    pub(crate) tag: Option<ObjectTag<'a>>,
}

impl PartialEq for InputObjectType<'_> {
    #[allow(unconditional_recursion)]
    fn eq(&self, other: &Self) -> bool {
        self.identifier.eq(&other.identifier)
    }
}

/// Object tags help differentiating objects during parsing / raw input data processing,
/// especially if complex object unions are present.
#[derive(Debug, Clone, PartialEq)]
pub enum ObjectTag<'a> {
    CompositeEnvelope,
    RelationEnvelope,
    // Holds the type against which a field can be compared
    FieldRefType(Box<InputType<'a>>),
    WhereInputType(ParentContainer),
    NestedToOneUpdateEnvelope,
}

#[derive(Debug, Default, PartialEq, Clone)]
pub struct InputObjectTypeConstraints<'a> {
    /// The maximum number of fields that can be provided.
    pub min_num_fields: Option<usize>,

    /// The minimum number of fields that must be provided.
    pub max_num_fields: Option<usize>,

    /// The fields against which the constraints should be applied.
    /// If `None`, constraints should be applied on _all_ fields on the input object type.
    pub fields: Option<Vec<Cow<'a, str>>>,
}

impl Debug for InputObjectType<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("InputObjectType")
            .field("identifier", &self.identifier)
            .field("constraints", &self.constraints)
            .field("fields", &"#Input Fields Cell#")
            .finish()
    }
}

impl<'a> InputObjectType<'a> {
    pub fn get_fields(&self) -> &[InputField<'a>] {
        self.fields.as_ref().map(|f| -> &[_] { f }).unwrap_or(&[])
    }

    pub(crate) fn set_fields(&mut self, f: impl FnOnce() -> Vec<InputField<'a>> + Send + Sync + 'a) {
        self.fields = Some(Arc::new(Lazy::new(Box::new(f))));
    }

    pub fn tag(&self) -> Option<&ObjectTag<'a>> {
        self.tag.as_ref()
    }

    pub fn find_field(&self, name: &str) -> Option<&InputField<'a>> {
        self.get_fields().iter().find(|f| f.name == name)
    }

    /// Require exactly one field of the possible ones to be in the input.
    pub(crate) fn require_exactly_one_field(&mut self) {
        self.set_max_fields(1);
        self.set_min_fields(1);
    }

    /// Require at least one field of the possible ones to be in the input.
    pub(crate) fn require_at_least_one_field(&mut self) {
        self.set_min_fields(1);
    }

    /// Require at most one field of the possible ones to be in the input.
    pub(crate) fn require_at_most_one_field(&mut self) {
        self.set_max_fields(1);
        self.set_min_fields(0);
    }

    /// Require a maximum of `max` fields to be present in the input.
    pub(crate) fn set_max_fields(&mut self, max: usize) {
        self.constraints.max_num_fields = Some(max);
    }

    /// Require a minimum of `min` fields to be present in the input.
    pub(crate) fn set_min_fields(&mut self, min: usize) {
        self.constraints.min_num_fields = Some(min);
    }

    pub(crate) fn apply_constraints_on_fields(&mut self, fields: Vec<Cow<'a, str>>) {
        self.constraints.fields = Some(fields);
    }

    pub(crate) fn set_tag(&mut self, tag: ObjectTag<'a>) {
        self.tag = Some(tag);
    }
}

#[derive(Debug, Clone)]
pub struct InputField<'a> {
    pub name: Cow<'a, str>,
    pub default_value: Option<DefaultKind>,

    field_types: Vec<InputType<'a>>,
    is_required: bool,
}

impl<'a> InputField<'a> {
    pub(crate) fn new(
        name: Cow<'a, str>,
        field_types: Vec<InputType<'a>>,
        default_value: Option<DefaultKind>,
        is_required: bool,
    ) -> InputField<'a> {
        InputField {
            name,
            default_value,
            field_types,
            is_required,
        }
    }

    pub fn field_types(&self) -> &[InputType<'a>] {
        &self.field_types
    }

    /// Indicates if the presence of the field on the higher input objects
    /// is required, but doesn't state whether or not the input can be null.
    pub fn is_required(&self) -> bool {
        self.is_required
    }

    /// Sets the field as optional (not required to be present on the input).
    pub(crate) fn optional(mut self) -> Self {
        self.is_required = false;
        self
    }

    /// Sets the field as optional (not required to be present on the input).
    pub(crate) fn required(mut self) -> Self {
        self.is_required = true;
        self
    }

    /// Sets the field as optional if the condition is true.
    pub(crate) fn optional_if(self, condition: bool) -> Self {
        if condition {
            self.optional()
        } else {
            self
        }
    }

    /// Sets the field as nullable (accepting null inputs).
    pub(crate) fn nullable(mut self) -> Self {
        // self.field_types = Box::new(|| {
        //     let f = &self.field_types;
        //     let mut fields = f();
        //     fields.push(InputType::null());
        //     fields
        // });
        self.field_types.push(InputType::null());
        self
    }

    /// Sets the field as nullable if the condition is true.
    pub(crate) fn nullable_if(self, condition: bool) -> Self {
        if condition {
            self.nullable()
        } else {
            self
        }
    }
}

#[derive(Clone)]
pub enum InputType<'a> {
    Scalar(ScalarType),
    Enum(EnumType),
    List(Box<InputType<'a>>),
    Object(InputObjectType<'a>),
}

impl<'a> PartialEq for InputType<'a> {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (InputType::Scalar(st), InputType::Scalar(ost)) => st.eq(ost),
            (InputType::Enum(_), InputType::Enum(_)) => true,
            (InputType::List(lt), InputType::List(olt)) => lt.eq(olt),
            (InputType::Object(obj), InputType::Object(oobj)) => obj == oobj,
            _ => false,
        }
    }
}

impl<'a> Debug for InputType<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Object(obj) => write!(f, "Object({obj:?})"),
            Self::Scalar(s) => write!(f, "{s:?}"),
            Self::Enum(e) => write!(f, "{e:?}"),
            Self::List(l) => write!(f, "{l:?}"),
        }
    }
}

impl<'a> InputType<'a> {
    pub(crate) fn list(containing: InputType<'a>) -> InputType<'a> {
        InputType::List(Box::new(containing))
    }

    pub(crate) fn object(containing: InputObjectType<'a>) -> InputType<'a> {
        InputType::Object(containing)
    }

    pub(crate) fn string() -> InputType<'a> {
        InputType::Scalar(ScalarType::String)
    }

    pub(crate) fn int() -> InputType<'a> {
        InputType::Scalar(ScalarType::Int)
    }

    pub(crate) fn bigint() -> InputType<'a> {
        InputType::Scalar(ScalarType::BigInt)
    }

    pub(crate) fn float() -> InputType<'a> {
        InputType::Scalar(ScalarType::Float)
    }

    pub(crate) fn decimal() -> InputType<'a> {
        InputType::Scalar(ScalarType::Decimal)
    }

    pub(crate) fn boolean() -> InputType<'a> {
        InputType::Scalar(ScalarType::Boolean)
    }

    pub(crate) fn date_time() -> InputType<'a> {
        InputType::Scalar(ScalarType::DateTime)
    }

    pub(crate) fn json() -> InputType<'a> {
        InputType::Scalar(ScalarType::Json)
    }

    pub(crate) fn json_list() -> InputType<'a> {
        InputType::Scalar(ScalarType::JsonList)
    }

    pub(crate) fn uuid() -> InputType<'a> {
        InputType::Scalar(ScalarType::UUID)
    }

    pub(crate) fn bytes() -> InputType<'a> {
        InputType::Scalar(ScalarType::Bytes)
    }

    pub(crate) fn null() -> InputType<'a> {
        InputType::Scalar(ScalarType::Null)
    }

    pub(crate) fn enum_type(containing: EnumType) -> InputType<'a> {
        InputType::Enum(containing)
    }

    pub fn is_json(&self) -> bool {
        matches!(
            self,
            Self::Scalar(ScalarType::Json) | Self::Scalar(ScalarType::JsonList)
        )
    }

    pub fn as_object(&self) -> Option<&InputObjectType<'a>> {
        if let Self::Object(v) = self {
            Some(v)
        } else {
            None
        }
    }

    pub fn as_list(&self) -> Option<&InputType<'a>> {
        if let Self::List(list) = self {
            Some(list)
        } else {
            None
        }
    }

    pub fn into_object(self) -> Option<InputObjectType<'a>> {
        match self {
            InputType::Object(obj) => Some(obj),
            _ => None,
        }
    }
}