import { Schema } from 'effect';
import {
    AccountName,
    Colors,
    Did,
    Email,
    Ordered,
    PhoneNumber,
    Represents,
    Sector,
    Site,
    TaxRate
} from '../types/bindings';
/** import macro { JSON } from "@playground/macro"; */

export class MacroUser {
    id: string;
    name: string;
    role: string;
    favoriteMacro: 'Derive' | 'JsonNative';
    since: string;

    apiToken: string;

    constructor(
        id: string,
        name: string,
        role: string,
        favoriteMacro: 'Derive' | 'JsonNative',
        since: string,
        apiToken: string
    ) {
        this.id = id;
        this.name = name;
        this.role = role;
        this.favoriteMacro = favoriteMacro;
        this.since = since;
        this.apiToken = apiToken;
    }

    static toString(value: MacroUser): string {
        return macroUserToString(value);
    }

    toJSON(): Record<string, unknown> {
        const result: Record<string, unknown> = {};
        result.id = this.id;
        result.name = this.name;
        result.role = this.role;
        result.favoriteMacro = this.favoriteMacro;
        result.since = this.since;
        result.apiToken = this.apiToken;
        return result;
    }
}

export function macroUserToString(value: MacroUser): string {
    const parts: string[] = [];
    parts.push('userId: ' + value.id);
    parts.push('name: ' + value.name);
    parts.push('role: ' + value.role);
    parts.push('favoriteMacro: ' + value.favoriteMacro);
    parts.push('since: ' + value.since);
    return 'MacroUser { ' + parts.join(', ') + ' }';
}

const showcaseUser = new MacroUser(
    'usr_2626',
    'Alya Vector',
    'Macro Explorer',
    'Derive',
    '2024-09-12',
    'svelte-secret-token'
);

export const showcaseUserSummary = MacroUser.toString(showcaseUser);
export const showcaseUserJson = showcaseUser.toObject();

export class Account extends Schema.Class<Account>('Account')({
    id: Schema.propertySignature(Schema.String).annotations({
        missingMessage: () => `'Id' is required`
    }),
    taxRate: Schema.propertySignature(
        Schema.Union(
            Schema.String.pipe(Schema.nonEmptyString()),
            TaxRate
        ).annotations({
            message: () => ({
                message: `Please enter a valid value`,
                override: true
            })
        })
    ).annotations({ missingMessage: () => `'Tax Rate' is required` }),
    site: Schema.propertySignature(
        Schema.Union(Schema.String.pipe(Schema.nonEmptyString()), Site).annotations(
            {
                message: () => ({
                    message: `Please enter a valid value`,
                    override: true
                })
            }
        )
    ).annotations({ missingMessage: () => `'Site' is required` }),
    salesRep: Schema.OptionFromNullishOr(
        Schema.Array(Represents).annotations({
            identifier: `RepresentsRef`
        }),
        null
    ),
    orders: Schema.propertySignature(
        Schema.Array(
            Ordered.annotations({
                identifier: `OrderedRef`
            })
        )
    ).annotations({ missingMessage: () => `'Orders' is required` }),
    activity: Schema.propertySignature(
        Schema.Array(
            Did.annotations({
                identifier: `DidRef`
            })
        )
    ).annotations({ missingMessage: () => `'Activity' is required` }),
    customFields: Schema.propertySignature(
        Schema.Array(
            Schema.Tuple(
                Schema.String.pipe(
                    Schema.nonEmptyString({ message: () => `Please enter a value` })
                ),
                Schema.String.pipe(
                    Schema.nonEmptyString({ message: () => `Please enter a value` })
                )
            )
        )
    ).annotations({ missingMessage: () => `'Custom Fields' is required` }),
    accountName: Schema.propertySignature(AccountName).annotations({
        missingMessage: () => `'Account Name' is required`
    }),
    sector: Schema.propertySignature(Sector).annotations({
        missingMessage: () => `'Sector' is required`
    }),
    memo: Schema.OptionFromNullishOr(
        Schema.String.pipe(
            Schema.nonEmptyString({ message: () => `Please enter a value` })
        ),
        null
    ),
    phones: Schema.propertySignature(Schema.Array(PhoneNumber)).annotations({
        missingMessage: () => `'Phones' is required`
    }),
    email: Schema.propertySignature(Email).annotations({
        missingMessage: () => `'Email' is required`
    }),
    leadSource: Schema.propertySignature(
        Schema.String.pipe(
            Schema.nonEmptyString({ message: () => `Please enter a value` })
        )
    ).annotations({ missingMessage: () => `'Lead Source' is required` }),
    colors: Schema.propertySignature(Colors).annotations({
        missingMessage: () => `'Colors' is required`
    }),
    needsReview: Schema.propertySignature(Schema.Boolean).annotations({
        missingMessage: () => `'Needs Review' is required`
    }),
    hasAlert: Schema.propertySignature(Schema.Boolean).annotations({
        missingMessage: () => `'Has Alert' is required`
    }),
    accountType: Schema.propertySignature(
        Schema.String.pipe(
            Schema.nonEmptyString({ message: () => `Please enter a value` })
        )
    ).annotations({ missingMessage: () => `'Account Type' is required` }),
    subtype: Schema.propertySignature(
        Schema.String.pipe(
            Schema.nonEmptyString({ message: () => `Please enter a value` })
        )
    ).annotations({ missingMessage: () => `'Subtype' is required` }),
    isTaxExempt: Schema.propertySignature(Schema.Boolean).annotations({
        missingMessage: () => `'Is Tax Exempt' is required`
    }),
    paymentTerms: Schema.propertySignature(
        Schema.String.pipe(
            Schema.nonEmptyString({ message: () => `Please enter a value` })
        )
    ).annotations({ missingMessage: () => `'Payment Terms' is required` }),
    tags: Schema.propertySignature(
        Schema.Array(
            Schema.String.pipe(
                Schema.nonEmptyString({ message: () => `Please enter a value` })
            )
        )
    ).annotations({ missingMessage: () => `'Tags' is required` }),
    dateAdded: Schema.propertySignature(Schema.DateTimeUtc).annotations({
        missingMessage: () => `'Date Added' is required`
    })
}) {}
