# Copyright 2018 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
- release_tracks: [ALPHA]
  help_text:
    brief: Create a new BigQuery table.
    description: |-
      Create a new BigQuery table.
      Create a table or view with a specified name. A view is a collection of rows
      selected by a query in a flag, and manipulated as a table. The dataset to contain
      the table or view must already exist, and must not contain a table or view with
      the specified name.
    examples: |
      The following command creates a table with ID  `my-table` in `my-dataset`:

        $ {command}  /projects/myproject/datasets/my-dataset/tables/my-table --description 'My New Table'

      The following command creates a view with ID `my-view` in dataset `my-other-dataset`:

        $ {command}  my-view --dataset my-other-dataset \
          --view 'SELECT field1, field3 FROM `my-project.my-other-dataset.my-table`'

  request:
    collection: bigquery.tables
    method: insert
    modify_request_hooks:
    - googlecloudsdk.command_lib.bq.hooks:ProcessTableOverwrite
    - googlecloudsdk.command_lib.bq.hooks:SetViewParameters
    - googlecloudsdk.command_lib.util.hooks.request_modifiers:SetFieldFromName:api_field=table.tableReference.tableId
  arguments:
    resource:
      help_text: The BigQuery table you want to create.
      spec: !REF googlecloudsdk.command_lib.bq.resources:table

    params:
    - _REF_: googlecloudsdk.command_lib.bq.flags:overwrite
    - _REF_: googlecloudsdk.command_lib.bq.flags:table.description
    - _REF_: googlecloudsdk.command_lib.bq.flags:table.expiration

    - group:
        mutex: true
        required: false
        help_text: Specify the table schema.
        params:
        - arg_name: schema
          metavar: FIELD_NAME=FIELD_TYPE
          api_field: table.schema.fields
          type:
            arg_dict:
              flatten: true
              spec:
              - api_field: name
              - api_field: type
          help_text: |-
            A comma-separated list of entries of the form FIELD_NAME[=FIELD_TYPE] specifying field names
            and types for the table being created. FIELD_TYPE defaults to string if not present.
            Possible FIELD_TYPES are `string`, `integer`, `float`, `boolean`, `record`, and `timestamp`.

            For more details on BigQuery schemas see: https://cloud.google.com/bigquery/docs/schemas.
        - arg_name: schema-file
          api_field: table.schema
          type: "googlecloudsdk.calliope.arg_parsers:FileContents:"
          processor: googlecloudsdk.command_lib.bq.hooks:BqTableSchemaFileProcessor
          help_text: |-
            The name of a JSON file containing a single object containing an array each element
            of which is an object with properties name, type, and, optionally a
            mode (one of: `NULLABLE`, `REQUIRED` or `REPEATED`), specifying a schema for the table
            being created. If mode is omitted the default is 'NULLABLE'.

            For example:
            {
              'schema':
                [
                  {
                    'name': 'field1',
                    'type': 'string',
                    'mode': 'REQUIRED'
                  },
                  {
                    'name': 'field2',
                    'type': 'integer',
                    'mode': 'REPEATED'
                  },
                  [
                    {
                      'name': 'fieldN',
                      'type': TYPE,
                      ['mode': MODE]
                    }
                    ...
                  ]
                ]
            }

            For more details on BigQuery schemas see: https://cloud.google.com/bigquery/docs/schemas.
        - group:
            help_text: Create a view instead of regular table.
            required: false
            params:
            - api_field: table.view.query
              arg_name: view
              required: true
              help_text: |-
                Create a view with this SQL query. (If this flag is not specified, a table is created.)
            - api_field: table.view.useLegacySql
              arg_name: use-legacy-sql
              action: store_true
              help_text: |-
                If specified, query will use BigQuery's legacy SQL syntax. If not specified, query will use
                BigQuery's standard SQL dialect by default (https://cloud.google.com/bigquery/sql-reference/).
