// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.

// Code generated (@generated) by entc, DO NOT EDIT.

package ent

import (
	"context"
	"fmt"
	"log"

	"github.com/facebookincubator/ent/examples/edgeindex/ent/migrate"

	"github.com/facebookincubator/ent/examples/edgeindex/ent/city"
	"github.com/facebookincubator/ent/examples/edgeindex/ent/street"

	"github.com/facebookincubator/ent/dialect"
	"github.com/facebookincubator/ent/dialect/sql"
)

// Client is the client that holds all ent builders.
type Client struct {
	config
	// Schema is the client for creating, migrating and dropping schema.
	Schema *migrate.Schema
	// City is the client for interacting with the City builders.
	City *CityClient
	// Street is the client for interacting with the Street builders.
	Street *StreetClient
}

// NewClient creates a new client configured with the given options.
func NewClient(opts ...Option) *Client {
	c := config{log: log.Println}
	c.options(opts...)
	return &Client{
		config: c,
		Schema: migrate.NewSchema(c.driver),
		City:   NewCityClient(c),
		Street: NewStreetClient(c),
	}
}

// Open opens a connection to the database specified by the driver name and a
// driver-specific data source name, and returns a new client attached to it.
// Optional parameters can be added for configuring the client.
func Open(driverName, dataSourceName string, options ...Option) (*Client, error) {
	switch driverName {
	case dialect.MySQL, dialect.Postgres, dialect.SQLite:
		drv, err := sql.Open(driverName, dataSourceName)
		if err != nil {
			return nil, err
		}
		return NewClient(append(options, Driver(drv))...), nil

	default:
		return nil, fmt.Errorf("unsupported driver: %q", driverName)
	}
}

// Tx returns a new transactional client.
func (c *Client) Tx(ctx context.Context) (*Tx, error) {
	if _, ok := c.driver.(*txDriver); ok {
		return nil, fmt.Errorf("ent: cannot start a transaction within a transaction")
	}
	tx, err := newTx(ctx, c.driver)
	if err != nil {
		return nil, fmt.Errorf("ent: starting a transaction: %v", err)
	}
	cfg := config{driver: tx, log: c.log, debug: c.debug}
	return &Tx{
		config: cfg,
		City:   NewCityClient(cfg),
		Street: NewStreetClient(cfg),
	}, nil
}

// Debug returns a new debug-client. It's used to get verbose logging on specific operations.
//
//	client.Debug().
//		City.
//		Query().
//		Count(ctx)
//
func (c *Client) Debug() *Client {
	if c.debug {
		return c
	}
	cfg := config{driver: dialect.Debug(c.driver, c.log), log: c.log, debug: true}
	return &Client{
		config: cfg,
		Schema: migrate.NewSchema(cfg.driver),
		City:   NewCityClient(cfg),
		Street: NewStreetClient(cfg),
	}
}

// Close closes the database connection and prevents new queries from starting.
func (c *Client) Close() error {
	return c.driver.Close()
}

// CityClient is a client for the City schema.
type CityClient struct {
	config
}

// NewCityClient returns a client for the City from the given config.
func NewCityClient(c config) *CityClient {
	return &CityClient{config: c}
}

// Create returns a create builder for City.
func (c *CityClient) Create() *CityCreate {
	return &CityCreate{config: c.config}
}

// Update returns an update builder for City.
func (c *CityClient) Update() *CityUpdate {
	return &CityUpdate{config: c.config}
}

// UpdateOne returns an update builder for the given entity.
func (c *CityClient) UpdateOne(ci *City) *CityUpdateOne {
	return c.UpdateOneID(ci.ID)
}

// UpdateOneID returns an update builder for the given id.
func (c *CityClient) UpdateOneID(id int) *CityUpdateOne {
	return &CityUpdateOne{config: c.config, id: id}
}

// Delete returns a delete builder for City.
func (c *CityClient) Delete() *CityDelete {
	return &CityDelete{config: c.config}
}

// DeleteOne returns a delete builder for the given entity.
func (c *CityClient) DeleteOne(ci *City) *CityDeleteOne {
	return c.DeleteOneID(ci.ID)
}

// DeleteOneID returns a delete builder for the given id.
func (c *CityClient) DeleteOneID(id int) *CityDeleteOne {
	return &CityDeleteOne{c.Delete().Where(city.ID(id))}
}

// Create returns a query builder for City.
func (c *CityClient) Query() *CityQuery {
	return &CityQuery{config: c.config}
}

// Get returns a City entity by its id.
func (c *CityClient) Get(ctx context.Context, id int) (*City, error) {
	return c.Query().Where(city.ID(id)).Only(ctx)
}

// GetX is like Get, but panics if an error occurs.
func (c *CityClient) GetX(ctx context.Context, id int) *City {
	ci, err := c.Get(ctx, id)
	if err != nil {
		panic(err)
	}
	return ci
}

// QueryStreets queries the streets edge of a City.
func (c *CityClient) QueryStreets(ci *City) *StreetQuery {
	query := &StreetQuery{config: c.config}
	id := ci.ID
	step := &sql.Step{}
	step.From.V = id
	step.From.Table = city.Table
	step.From.Column = city.FieldID
	step.To.Table = street.Table
	step.To.Column = street.FieldID
	step.Edge.Rel = sql.O2M
	step.Edge.Inverse = false
	step.Edge.Table = city.StreetsTable
	step.Edge.Columns = append(step.Edge.Columns, city.StreetsColumn)
	query.sql = sql.Neighbors(ci.driver.Dialect(), step)

	return query
}

// StreetClient is a client for the Street schema.
type StreetClient struct {
	config
}

// NewStreetClient returns a client for the Street from the given config.
func NewStreetClient(c config) *StreetClient {
	return &StreetClient{config: c}
}

// Create returns a create builder for Street.
func (c *StreetClient) Create() *StreetCreate {
	return &StreetCreate{config: c.config}
}

// Update returns an update builder for Street.
func (c *StreetClient) Update() *StreetUpdate {
	return &StreetUpdate{config: c.config}
}

// UpdateOne returns an update builder for the given entity.
func (c *StreetClient) UpdateOne(s *Street) *StreetUpdateOne {
	return c.UpdateOneID(s.ID)
}

// UpdateOneID returns an update builder for the given id.
func (c *StreetClient) UpdateOneID(id int) *StreetUpdateOne {
	return &StreetUpdateOne{config: c.config, id: id}
}

// Delete returns a delete builder for Street.
func (c *StreetClient) Delete() *StreetDelete {
	return &StreetDelete{config: c.config}
}

// DeleteOne returns a delete builder for the given entity.
func (c *StreetClient) DeleteOne(s *Street) *StreetDeleteOne {
	return c.DeleteOneID(s.ID)
}

// DeleteOneID returns a delete builder for the given id.
func (c *StreetClient) DeleteOneID(id int) *StreetDeleteOne {
	return &StreetDeleteOne{c.Delete().Where(street.ID(id))}
}

// Create returns a query builder for Street.
func (c *StreetClient) Query() *StreetQuery {
	return &StreetQuery{config: c.config}
}

// Get returns a Street entity by its id.
func (c *StreetClient) Get(ctx context.Context, id int) (*Street, error) {
	return c.Query().Where(street.ID(id)).Only(ctx)
}

// GetX is like Get, but panics if an error occurs.
func (c *StreetClient) GetX(ctx context.Context, id int) *Street {
	s, err := c.Get(ctx, id)
	if err != nil {
		panic(err)
	}
	return s
}

// QueryCity queries the city edge of a Street.
func (c *StreetClient) QueryCity(s *Street) *CityQuery {
	query := &CityQuery{config: c.config}
	id := s.ID
	step := &sql.Step{}
	step.From.V = id
	step.From.Table = street.Table
	step.From.Column = street.FieldID
	step.To.Table = city.Table
	step.To.Column = city.FieldID
	step.Edge.Rel = sql.M2O
	step.Edge.Inverse = true
	step.Edge.Table = street.CityTable
	step.Edge.Columns = append(step.Edge.Columns, street.CityColumn)
	query.sql = sql.Neighbors(s.driver.Dialect(), step)

	return query
}
